Results 1 to 17 of 17

Thread: [RESOLVED] [2008] Random Number

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Posts
    22

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Random Number

    Quote 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:
    1. Dim rng As New Random
    2. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Random Number

    Quote 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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    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!

  8. #8
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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'

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Random Number

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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

  11. #11
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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.

  12. #12

  13. #13
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote Originally Posted by NickThissen
    Figures

    People here are trying to help you, so I suggest you take their help.
    ok...

  14. #14
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: [2008] Random Number

    Quote 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

  17. #17
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. 'New random class
    2. Dim rnd As New Random
    3.  
    4. 'Number of letter A (the c after "A" means it is a character instead of a string)
    5. Dim numberA As Integer = Convert.ToInt32("A"c)
    6.  
    7. 'Number of letter Z
    8. Dim numberZ As Integer = Convert.ToInt32("Z"c)
    9.  
    10. 'Generate random number between these two
    11. ' numberZ + 1 because the second argument is exclusive
    12. Dim i As Integer = rnd.Next(numberA, numberZ + 1)
    13.  
    14. 'Finally, convert the random number i back into a character
    15. Dim randomChar As Char = Convert.ToChar(i)

    As said, this can be combined into:
    vb.net Code:
    1. Dim rnd As New Random
    2. 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
  •  



Click Here to Expand Forum to Full Width