|
-
Jan 15th, 2009, 11:50 PM
#1
Thread Starter
Junior Member
[RESOLVED] [2008] Random Number
I have a function called DieRoll. In the function I have the following setup.
Code:
Dim objRandom as New Random
Dim intRoll as Integer
intRoll = objRandom.Next(1,7)
Return intRoll
Everything works as expected as far as generating a random number (1 - 6) is concerned, except for one problem. If I have a series of calls to the function one after another, the numbers come up the same. Is there a way to make sure that each call to the function is much more random?
-
Jan 15th, 2009, 11:58 PM
#2
Re: [2008] Random Number
The problem is undoubtedly that you're creating a new Random object each time and calling Next only once. In that case each Random object uses the system time as a seed, which will not have changed sufficiently to create a new pseudo-random sequence each time. You should be using a single Random object and calling its Next method multiple times. You might like to follow the Dice link in my signature.
-
Jan 16th, 2009, 09:14 AM
#3
Junior Member
Re: [2008] Random Number
 Originally Posted by frozenbrain
I have a function called DieRoll. In the function I have the following setup.
Code:
Dim objRandom as New Random
Dim intRoll as Integer
intRoll = objRandom.Next(1,7)
Return intRoll
Everything works as expected as far as generating a random number (1 - 6) is concerned, except for one problem. If I have a series of calls to the function one after another, the numbers come up the same. Is there a way to make sure that each call to the function is much more random?
i have something like that code.
i need like one the does letters not numbers becouse i already have that kind of code but thanks for your help
-
Jan 16th, 2009, 09:22 AM
#4
Re: [2008] Random Number
 Originally Posted by Choober
i have something like that code.
i need like one the does letters not numbers becouse i already have that kind of code but thanks for your help 
Generating random values means generating random numbers plain and simple. How you use those numbers is up to you though. Maybe you could use it as an index into an array or collection to select a random item. Maybe you could use it as the code point for a character. This will generate a random upper-case letter:
vb.net Code:
Dim rng As New Random
Dim ch As Char = Convert.ToChar(rng.Next(Convert.ToInt32("A"c), Convert.ToInt32("Z"c) + 1))
You get the numerical value of A and Z, generate a random number in that range and then convert that number to a character. It simply comes down to how you use the number.
-
Jan 16th, 2009, 09:36 AM
#5
Re: [2008] Random Number
Having recently built a random password generator... I used this to get a random letter between A and z (keep in mind that A and a are not the same, I did mine this way because the passwords needed to be case sensitive).
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("z", 0))
if you want all letters in caps:
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("Z", 0))
or all lower
alphaNum = rndNumber.Next(Char.ConvertToUtf32("a", 0), Char.ConvertToUtf32("z", 0))
-tg
-
Jan 16th, 2009, 09:44 AM
#6
Re: [2008] Random Number
 Originally Posted by techgnome
Having recently built a random password generator... I used this to get a random letter between A and z (keep in mind that A and a are not the same, I did mine this way because the passwords needed to be case sensitive).
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("z", 0))
if you want all letters in caps:
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("Z", 0))
or all lower
alphaNum = rndNumber.Next(Char.ConvertToUtf32("a", 0), Char.ConvertToUtf32("z", 0))
-tg
You'll note that there's a +1 at the end of my code. Remember that, when calling Next, the minimum is inclusive but the maximum is exclusive. As a result, your passwords will never contain a "z".
-
Jan 16th, 2009, 09:47 AM
#7
Junior Member
Re: [2008] Random Number
this just might be what i wanted but. where do i put this code at? oh an do i add anything else thanks so much!
-
Jan 16th, 2009, 09:52 AM
#8
Junior Member
Re: [2008] Random Number
 Originally Posted by techgnome
Having recently built a random password generator... I used this to get a random letter between A and z (keep in mind that A and a are not the same, I did mine this way because the passwords needed to be case sensitive).
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("z", 0))
if you want all letters in caps:
alphaNum = rndNumber.Next(Char.ConvertToUtf32("A", 0), Char.ConvertToUtf32("Z", 0))
or all lower
alphaNum = rndNumber.Next(Char.ConvertToUtf32("a", 0), Char.ConvertToUtf32("z", 0))
-tg
thanks but it gives me a error if i put it in the timer? something about "name Rndnumber is not declared'
-
Jan 16th, 2009, 09:52 AM
#9
Re: [2008] Random Number
 Originally Posted by Choober
this just might be what i wanted but. where do i put this code at? oh an do i add anything else thanks so much! 
I see people ask where they put code a lot. Where do you put a chair? You put it where you want to sit. Where do you put some code? You put it where you want it to be executed. That could be many places, depending on your specific application. Do you want this random letter generated when the user clicks a Button? Then you put the code in that Button's Click event handler. That's just one example. It's up to you where you put it because it's up to you where you want it executed.
Also, the example I provided is a simplification. As I already said to the OP, you normally don't create the Random object where you use it. You'll usually create it at the class level so the same object can be accessed multiple times and from multiple places.
Having said all that, the obvious option to me is to adapt my Die class to produce letters instead of numbers. You simply add some code on top of the existing number generator to convert the generated value to a Char, which myself and tg have demonstrated.
-
Jan 16th, 2009, 09:59 AM
#10
Junior Member
Re: [2008] Random Number
 Originally Posted by jmcilhinney
I see people ask where they put code a lot. Where do you put a chair? You put it where you want to sit. Where do you put some code? You put it where you want it to be executed. That could be many places, depending on your specific application. Do you want this random letter generated when the user clicks a Button? Then you put the code in that Button's Click event handler. That's just one example. It's up to you where you put it because it's up to you where you want it executed.
Also, the example I provided is a simplification. As I already said to the OP, you normally don't create the Random object where you use it. You'll usually create it at the class level so the same object can be accessed multiple times and from multiple places.
Having said all that, the obvious option to me is to adapt my Die class to produce letters instead of numbers. You simply add some code on top of the existing number generator to convert the generated value to a Char, which myself and tg have demonstrated.
don't get a attitude with me
-
Jan 16th, 2009, 10:07 AM
#11
Junior Member
Re: [2008] Random Number
 Originally Posted by jmcilhinney
I see people ask where they put code a lot. Where do you put a chair? You put it where you want to sit. Where do you put some code? You put it where you want it to be executed. That could be many places, depending on your specific application. Do you want this random letter generated when the user clicks a Button? Then you put the code in that Button's Click event handler. That's just one example. It's up to you where you put it because it's up to you where you want it executed.
Also, the example I provided is a simplification. As I already said to the OP, you normally don't create the Random object where you use it. You'll usually create it at the class level so the same object can be accessed multiple times and from multiple places.
Having said all that, the obvious option to me is to adapt my Die class to produce letters instead of numbers. You simply add some code on top of the existing number generator to convert the generated value to a Char, which myself and tg have demonstrated.
and all im saying is theres a error in the other guys code... you dont need to get all upset over me asking a question and i'm only 14.
-
Jan 16th, 2009, 10:19 AM
#12
Re: [2008] Random Number
 Originally Posted by Choober
and i'm only 14.
Figures
People here are trying to help you, so I suggest you take their help.
-
Jan 16th, 2009, 10:21 AM
#13
Junior Member
Re: [2008] Random Number
 Originally Posted by NickThissen
Figures
People here are trying to help you, so I suggest you take their help.
ok...
-
Jan 16th, 2009, 10:23 AM
#14
Junior Member
Re: [2008] Random Number
 Originally Posted by jmcilhinney
I see people ask where they put code a lot. Where do you put a chair? You put it where you want to sit. Where do you put some code? You put it where you want it to be executed. That could be many places, depending on your specific application. Do you want this random letter generated when the user clicks a Button? Then you put the code in that Button's Click event handler. That's just one example. It's up to you where you put it because it's up to you where you want it executed.
Also, the example I provided is a simplification. As I already said to the OP, you normally don't create the Random object where you use it. You'll usually create it at the class level so the same object can be accessed multiple times and from multiple places.
Having said all that, the obvious option to me is to adapt my Die class to produce letters instead of numbers. You simply add some code on top of the existing number generator to convert the generated value to a Char, which myself and tg have demonstrated.
hmm do you have like a AIM or something
-
Jan 16th, 2009, 10:56 AM
#15
Re: [2008] Random Number
OK.
Lesson 1: Don't EVER just blindly copy code w/o understanding what it is. The error is "rndNumber" OK great.... what do you think "rndNumber" is?
Lesson 2: That wasn't attitude. Trust me, I've seen attitude, that wasn't it.
Lesson 3: People around here don't jsut chuck out their aim, aol, icq, jabber what ever handles out to jsut anyone... these forums are about helping people... don't take the conversations private because you got your nose in a tiwst. That's not beneficial to the next 14yr old that comes through here looking for help. Keep it out in the open please.
Lesson 4: If you haven't already, might want to look for a book on VB.NET... or at least some more tutorials.
Lesson 5: In case you hadn't figured it out, these forums thrive on HELPING people... not giving out code. All too often people are asking for help with homework (not sayin that you are, just illustrating a point) so for us to jsut give away the correct and working code does those people a disservice. This is a teaching forum, we'll help people to learn how to find answers (which is why often you'll see posts that suggest a search of some kind). Sometimes that's all people need, to be pointed in the right direction. Other times it takes a little more prodding (this thread being one of those).
Lesson 6: Learn how to ask questions.
thanks but it gives me a error if i put it in the timer? something about "name Rndnumber is not declared'
isn't a question, that's a statement. OK... so now I'm going to ask a question (and this takes us back to lesson 1) If something isn't declared, what do you need to do?
-tg
-
Jan 16th, 2009, 12:03 PM
#16
Junior Member
Re: [2008] Random Number
 Originally Posted by techgnome
OK.
Lesson 1: Don't EVER just blindly copy code w/o understanding what it is. The error is "rndNumber" OK great.... what do you think "rndNumber" is?
Lesson 2: That wasn't attitude. Trust me, I've seen attitude, that wasn't it.
Lesson 3: People around here don't jsut chuck out their aim, aol, icq, jabber what ever handles out to jsut anyone... these forums are about helping people... don't take the conversations private because you got your nose in a tiwst. That's not beneficial to the next 14yr old that comes through here looking for help. Keep it out in the open please.
Lesson 4: If you haven't already, might want to look for a book on VB.NET... or at least some more tutorials.
Lesson 5: In case you hadn't figured it out, these forums thrive on HELPING people... not giving out code. All too often people are asking for help with homework (not sayin that you are, just illustrating a point) so for us to jsut give away the correct and working code does those people a disservice. This is a teaching forum, we'll help people to learn how to find answers (which is why often you'll see posts that suggest a search of some kind). Sometimes that's all people need, to be pointed in the right direction. Other times it takes a little more prodding (this thread being one of those).
Lesson 6: Learn how to ask questions. isn't a question, that's a statement. OK... so now I'm going to ask a question (and this takes us back to lesson 1) If something isn't declared, what do you need to do?
-tg
well i know RND means random & i'm going to take some classes about it soon and i'm taking notes from you becouse i really want to be a programmer and it might look like i don't know how to code but iv try about everything to get this to work and i figure out how to make the number go random like that but i can't figure out how to make letters but thanks for your help
-
Jan 16th, 2009, 12:12 PM
#17
Re: [2008] Random Number
Two people in this thread have already showed you how to make random letters. It is pretty much the same as random numbers, except that you now convert the random number into a character (letter). You see, every character has a corresponding number in the ASCII chart. VB can convert a number to a character (and back). So if you generate the random number "81" for example, and convert that to a letter, you get a Q.
The letters A to Z correspond to the numbers 65 to 90. So you would have to generate a random number between 65 and 91 (65 inclusive, 91 exclusive, so actually 90) and convert that to a character. However, to prevent you from having to look up the numbers (65 to 90) you can also let VB convert the letter "A" into a number (which yields 65) and the letter "Z" (yields 90).
Combining all this, we can write a long piece of code that can eventually be combined into what jmcthingy already gave you.
vb.net Code:
'New random class Dim rnd As New Random 'Number of letter A (the c after "A" means it is a character instead of a string) Dim numberA As Integer = Convert.ToInt32("A"c) 'Number of letter Z Dim numberZ As Integer = Convert.ToInt32("Z"c) 'Generate random number between these two ' numberZ + 1 because the second argument is exclusive Dim i As Integer = rnd.Next(numberA, numberZ + 1) 'Finally, convert the random number i back into a character Dim randomChar As Char = Convert.ToChar(i)
As said, this can be combined into:
vb.net Code:
Dim rnd As New Random Dim ch As Char = Convert.ToChar(rnd.Next(Convert.ToInt32("A"c), Convert.ToInt32("Z"c) + 1))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|