Results 1 to 29 of 29

Thread: Random Numbers -- Not so random?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Random Numbers -- Not so random?

    Ok first of all, I realized there not really random.

    For example, I used RND alone, lets say for exmample:

    Code:
    Rand = Int((High - Low + 1) * Rnd) + Low
    So lets say I input for high 100, and 1 for low. So you can get 1 to 10 as a random number. I start up the project, all is fine. I call the random number code twice and get these two numbers:

    Roll 1 = 71
    Roll 2 = 54

    Seems random enough. I shut it down and rerun it, yet I get the same two numbers. So I went to look around. People state to add RANDOMIZE, simple enough. I do so, bam -- added.

    The problem is in most cases it's generally STILL like there in a special sequence. For example, I run it the first time:

    Roll 1 = 57
    Roll 2 = 48

    .. Ok lets run it again.

    Roll 1 = 59
    Roll 2 = 50

    .. Again

    Roll 1 = 10
    Roll 2 = 1

    What?! Whats going on here? It feel like roll 2 will never win? Looking at it. It's still not generating RANDOM coding. its alway 9 short ! (Somethings I think either 10 or 8 depending on rounding? Not exactly sure but its generally the same). The only time Roll 2 will win is when roll 1 is less then 9 I believe, because ROll2 will start right back up at 99 and go down.. So it would be for example:

    Roll 1: 5
    Roll 2: 96

    Same 9, (basicly in this case less.)

    What I'm trying to ask is I've seen people use system time to solve this, to generate more random numbers. I'm thinking that might solve the problem. The problem is some numbers will be called at almost the SAME time. So what can I do to make the numbers TOTALLY random? At least to the point where Roll 2 has a random chance of winning. System time? A timer? Thanks!

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Random Numbers -- Not so random?

    And by that I mean using some sort of seed... thanks:P

  3. #3
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: Random Numbers -- Not so random?

    Your code is random(pseudo).. having two random numbers being 9 numbers apart 3 times doesn't mean you aren't getting 'random' numbers. The computer doesn't read it as two numbers, so there's no way it can decide to just give a values 9 less than the first roll.

    Put it in a Do Loop, you'll see that you just hit a stroke of luck. As for seeding, use:
    Randomize [seed]. I'm not sure what seed it uses as default, though

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Random Numbers -- Not so random?

    Randomize uses the system clock by default, it's equivalent to Randomize Timer.
    Call it once.

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Random Numbers -- Not so random?

    VB's Rnd function isn't that random you will sometimes get the same numbers together.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim Rand As Integer
    4. Dim High As Integer
    5. Dim Low As Integer
    6. Dim N As Integer
    7.  
    8. Randomize Timer
    9. High = 100
    10. Low = 1
    11. For N = 1 To 2
    12.     Rand = Int((High - Low + 1) * Rnd + Low)
    13.     Print Rand
    14. Next
    15.  
    16. End Sub
    But this code appears to work ok. You can call Randomize on Form_Load.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by Keithuk
    VB's Rnd function isn't that random you will sometimes get the same numbers together.
    I think the whole point of being random is that you get the same number sometimes...

    Unless all this time I missunderstood the meaning of random...

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Random Numbers -- Not so random?

    Keith thank you, also everyone else thanks. :P
    Seems to be working now More random now.. :P

    Just for everyones reference, I used:

    Public Function Rand(ByVal Low As Long, _
    ByVal High As Long) As Long
    Dim N As Integer

    Randomize Timer
    High = 100
    Low = 1
    For N = 1 To 2
    Rand = Int((High - Low + 1) * Rnd + Low)
    Next

    End Function

    P.S. Yeah Michael need to get the same number sometimes. long as its RANDOM ;p
    Last edited by Valleriani; Dec 25th, 2006 at 02:11 PM.

  8. #8
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Random Numbers -- Not so random?

    Thats ok, I was just suggesting that you can have the same numbers sometimes thats why it doesn't appear very random.

    Merry Christmas.
    Last edited by Keithuk; Dec 25th, 2006 at 03:30 PM.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Random Numbers -- Not so random?

    By now you have realised that you need to use Randomize in your app to (randomly) seed the generator. That said, you only need to use it once. For Example in the Form_Load() Event (and not every time you require Rnd().)
    Eg:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     [b]Randomize Timer[/b]
    5. End Sub
    6.  
    7. Public Function Rand(ByVal Low As Long, _
    8. ByVal High As Long) As Long
    9. Dim N As Integer
    10.  
    11. High = 100
    12. Low = 1
    13. For N = 1 To 2
    14. Rand = Int((High - Low + 1) * Rnd + Low)
    15. Next
    16.  
    17. End Function

  10. #10
    Fanatic Member newprogram's Avatar
    Join Date
    Apr 2006
    Location
    in your basement
    Posts
    769

    Re: Random Numbers -- Not so random?

    Sweet code
    Quote Originally Posted by Bruce Fox
    By now you have realised that you need to use Randomize in your app to (randomly) seed the generator. That said, you only need to use it once. For Example in the Form_Load() Event (and not every time you require Rnd().)
    Eg:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     [b]Randomize Timer[/b]
    5. End Sub
    6.  
    7. Public Function Rand(ByVal Low As Long, _
    8. ByVal High As Long) As Long
    9. Dim N As Integer
    10.  
    11. High = 100
    12. Low = 1
    13. For N = 1 To 2
    14. Rand = Int((High - Low + 1) * Rnd + Low)
    15. Next
    16.  
    17. End Function
    Live life to the fullest!!

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Random Numbers -- Not so random?

    The code I cut and pasted has an unwanted For/Next portion, It appears to have been used by the poster to test the results:
    VB Code:
    1. 'This:
    2.     For N = 1 To 2
    3.         Rand = Int((High - Low + 1) * Rnd + Low)
    4.     Next
    5.  
    6. 'can be replaced by this:
    7.     Rand = Int((High - Low + 1) * Rnd + Low)

  12. #12
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by Bruce Fox
    VB Code:
    1. Public Function Rand(ByVal Low As Long, _
    2. ByVal High As Long) As Long
    3. Dim N As Integer
    4.  
    5. High = 100
    6. Low = 1
    7. For N = 1 To 2
    8. Rand = Int((High - Low + 1) * Rnd + Low)
    9. Next
    10.  
    11. End Function
    Thats all well and good Bruce but you have Low and High as parameters but I've already stated High = 100 and Low - 1. There isn't much point have a For/Next loop because its a function.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Random Numbers -- Not so random?

    if anyone's interested, here was a 'discussion' about VB's Rnd and Randomize functions, which has a fair few links to other information on PRNGs.

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by Keithuk
    Thats all well and good Bruce but you have Low and High as parameters but I've already stated High = 100 and Low - 1. There isn't much point have a For/Next loop because its a function.
    Like I said, I just cut and pasted the original posters code to show that you only need the Randomize statement used once..... That was my point
    I also mentioned that there was redundant code that the poster probably used to test this method.

    The Function was not mine to begin with


    Cheers,

  15. #15
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by CVMichael
    I think the whole point of being random is that you get the same number sometimes...

    Unless all this time I missunderstood the meaning of random...
    Random doesn't necessary mean Unique and that's the difference.
    Random means give me a number from this range - that's all. So it (the Rnd function) returns any number...

  16. #16
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random Numbers -- Not so random?

    Would calling the Randomize function at a random time every day increase the randomness of the Rnd function? For example calling it everyday at the time of sunset or sunrise which varies on a day to day basis.

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Random Numbers -- Not so random?

    That's basically what it does already.. if you don't specify a parameter for Randomize, it defaults to the system Timer, which is the current time of day (including fractional seconds).

    While Rnd isn't truly random, using Randomize just once at the start of your program is enough to make it seem like it is (even if the computer is set to boot at a certain time, and your program runs on startup).

  18. #18
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Random Numbers -- Not so random?

    Ive seen a cracking way to get a random number on a piece of software. It brings up a box and asks you to randomly move your mouse around it. I guess it randomly (pff) selects locations your mouse is at and then uses one of these random figures for a seed. The only randomness in computing is humans, simple idea I thought.

  19. #19
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by si_the_geek
    That's basically what it does already.. if you don't specify a parameter for Randomize, it defaults to the system Timer, which is the current time of day (including fractional seconds).

    While Rnd isn't truly random, using Randomize just once at the start of your program is enough to make it seem like it is (even if the computer is set to boot at a certain time, and your program runs on startup).
    Got it. Thanks for the information si_the_geek. Does it also use the date or just the time?

    The reason I call Randomize around every 24 hours is that my program (an alarm application) may be left on for long periods of time (the computer doesn't get turned off) like computers at work. So I thought by doing this I could increase its randomness.

  20. #20
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Random Numbers -- Not so random?

    hi, I think the seed is limited to 2 bytes, or at least if you use a larger number it only uses 2 bytes worth of that number. Rnd will repeat itself after 16777116 (2^24) calls. Any seeding you do just starts the sequence from a different place... which in effect can make it less random if you do it lots of times.

  21. #21
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Random Numbers -- Not so random?

    It's just the number of seconds since midnight.

    As to programs running for long periods, it is irrelevant - the numbers are more random if you only call Randomize just once when your app starts. See the link in post #13 for explanations.

  22. #22
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by Hassan Basri
    Got it. Thanks for the information si_the_geek. Does it also use the date or just the time?

    The reason I call Randomize around every 24 hours is that my program (an alarm application) may be left on for long periods of time (the computer doesn't get turned off) like computers at work. So I thought by doing this I could increase its randomness.
    You do not need to call RANDOMIZE a second time ever - calling it the first time set's the seed location. From that point forward you get a nice random order of values.

    Calling RANDOMIZE a second time will reset the seed location - potentially backing you into a range of random values you already received.

    Basic rule - RANDOMIZE once and only once to set the seed location.

    It's been said in at least 3 or 4 posts in this thread already - but apparently it's not being fully understood.

    btw - we actually use the RND function more often without RANDOMIZE - as when we want to get a random order of students, for instance, but we always want that random order to be exactly the same. When an algorithm for scheduling is run on another day, for example, and the users have moved some classes around to see if they get better seat placement, giving that algorithm students in a different order would be counter productive.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  23. #23
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Random Numbers -- Not so random?

    Quote Originally Posted by Valleriani
    So what can I do to make the numbers TOTALLY random?
    You'd have to monitor the results of a truly random system. Something like which atom is the next one to split in a fissile substance. (Or is that really random? We don't know.)
    At least to the point where Roll 2 has a random chance of winning.
    That's been answered a few times, so I won't.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  24. #24
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random Numbers -- Not so random?

    What about after the computer goes into Stand-by mode, then comes out of stand-by, should I call Randomize then again, or just in the load event once in the application and never again? Thanks for the information everybody.

  25. #25
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Random Numbers -- Not so random?

    what are you trying to do with the random numbers?

  26. #26
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random Numbers -- Not so random?

    I have an alarm application, there are several alarms, one of them is random so it plays a random alarm from the list. I want the ramdomness maximised. So if the computer goes into stand-by and comes out do I call the Randomize again?

  27. #27
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Random Numbers -- Not so random?

    No, the program still has the current seed.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  28. #28
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Random Numbers -- Not so random?

    in that case don't worry about the randomness of vb's PRNG.

    if you were using it for encryption then you'd have a right to be concerned - but to the user there's going to be no discernible pattern (unless they're rain man or something)

  29. #29
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random Numbers -- Not so random?

    All right thanks guys.

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