Results 1 to 9 of 9

Thread: Generate a random number

  1. #1

    Thread Starter
    Addicted Member Garrett19212's Avatar
    Join Date
    Jan 2005
    Location
    US
    Posts
    220

    Generate a random number

    How can I generate a random number between 50-200.

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

    Re: Generate a random number

    You would use the Random class. It is a good idea to provide a seed to the constructor, but be aware that the same seed will ALWAYS produce the same psuedo-random sequence of numbers. For that reason, it is usual to provide a time-based seed like this:
    VB Code:
    1. Dim myRandom As New Random(CInt(Date.Now.Ticks Mod Integer.MaxValue))
    2.         Dim myRandomInteger As Integer = myRandom.Next(50, 201)
    Note the way the limits are expressed, i.e. the number returned is >= the minimum but < the maximum. Also note that you DO NOT create a new Random object to get another random number. You just keep calling Next on the same Random object. There are other methods too to get random Bytes and a random Double.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Generate a random number

    It has been pointed out to me in a different thread that seeding the Random object is unnecessary as the constructor that takes no arguments uses the system clock to seed the object anyway, probably in exactly the same way.
    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

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Generate a random number

    Actually this is something that has interested me, as people have argued .net doesnt return good random.. and i know vb6 didnt.. i would suggest using Jm's method whilse also including mouse x,y and cpu usage or some other fluxuating value similar to that, as you can never be completely random using a timer.. but with user input as an impossible factor, and system value influenced by many things, it would truely be randomized.

    Infact i might try it out and wack it in the codebank if i get bored.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Generate a random number

    Hi,

    This subject has been discussed many times in this forum and is already in the codebank. You guys are making it too complicated and the problem with the complicated approach is that you often get the same sequence every time you switch the computer on. The random function is as random as anyone will ever require.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Generate a random number

    Quote Originally Posted by taxes
    Hi,

    This subject has been discussed many times in this forum and is already in the codebank. You guys are making it too complicated and the problem with the complicated approach is that you often get the same sequence every time you switch the computer on. The random function is as random as anyone will ever require.
    I just created one which does not follow the same pattern every time, i ran a test, two instances of my new class, two instances of the old.. of course, the old one, which accepts a seed and follows a pattern from then on returned two identical lists.. and mine returned two different lists, i think that hits the nail on the head.. i'll check out the ones already in codebank before i post up
    Last edited by Phill64; Aug 30th, 2005 at 04:03 PM.

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Generate a random number

    Quote Originally Posted by Phill64
    I just created one which does not follow the same pattern every time, i ran a test, two instances of my new class, two instances of the old.. of course, the old one, which accepts a seed and follows a pattern from then on returned two identical lists.. and mine returned two different lists, i think that hits the nail on the head.. i'll check out the ones already in codebank before i post up
    Hi,

    Make sure you test it by comparing the results before and after rebooting.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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

    Re: Generate a random number

    Using the seed that I suggested will give you Integer.MaxValue different psuedo-random sequences. Rebooting will not affect its randomness, nor will using it at exactly the same time each day. I wasn't aware of this previously but it seems that using the constructor that takes no arguments actually uses this same time-based method of seeding the sequence, so why would you need to do anything else?

    Edit:
    Actually, I can't confirm that it uses that same method, but just that it uses some time-based seed value. It seems logical that it would use the same method I suggested given that it is used elsewhere in the help, but then my logic is not necessarily someone else's.
    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

  9. #9
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Generate a random number

    Taxes, the rebooting thing will not be a problem, i have tested the original function and my own side by side, 2 instances of each, both created at the same time, of course, the original returns identicle results, mine does not.

    here's the codebank thread
    http://www.vbforums.com/showthread.php?t=357926

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