|
-
Dec 25th, 2006, 07:00 AM
#1
Thread Starter
Member
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!
-
Dec 25th, 2006, 07:04 AM
#2
Thread Starter
Member
Re: Random Numbers -- Not so random?
And by that I mean using some sort of seed... thanks:P
-
Dec 25th, 2006, 07:10 AM
#3
Hyperactive Member
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
-
Dec 25th, 2006, 07:13 AM
#4
Re: Random Numbers -- Not so random?
Randomize uses the system clock by default, it's equivalent to Randomize Timer.
Call it once.
-
Dec 25th, 2006, 07:20 AM
#5
Re: Random Numbers -- Not so random?
VB's Rnd function isn't that random you will sometimes get the same numbers together.
VB Code:
Private Sub Command1_Click()
Dim Rand As Integer
Dim High As Integer
Dim Low As Integer
Dim N As Integer
Randomize Timer
High = 100
Low = 1
For N = 1 To 2
Rand = Int((High - Low + 1) * Rnd + Low)
Print Rand
Next
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.
-
Dec 25th, 2006, 11:20 AM
#6
Re: Random Numbers -- Not so random?
 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...
-
Dec 25th, 2006, 02:05 PM
#7
Thread Starter
Member
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.
-
Dec 25th, 2006, 03:21 PM
#8
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.
-
Dec 25th, 2006, 08:09 PM
#9
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:
Option Explicit
Private Sub Form_Load()
[b]Randomize Timer[/b]
End Sub
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Dim N As Integer
High = 100
Low = 1
For N = 1 To 2
Rand = Int((High - Low + 1) * Rnd + Low)
Next
End Function
-
Dec 25th, 2006, 08:15 PM
#10
Fanatic Member
Re: Random Numbers -- Not so random?
Sweet code
 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:
Option Explicit
Private Sub Form_Load()
[b]Randomize Timer[/b]
End Sub
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Dim N As Integer
High = 100
Low = 1
For N = 1 To 2
Rand = Int((High - Low + 1) * Rnd + Low)
Next
End Function
Live life to the fullest!!
-
Dec 25th, 2006, 08:25 PM
#11
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:
'This:
For N = 1 To 2
Rand = Int((High - Low + 1) * Rnd + Low)
Next
'can be replaced by this:
Rand = Int((High - Low + 1) * Rnd + Low)
-
Dec 26th, 2006, 06:49 AM
#12
Re: Random Numbers -- Not so random?
 Originally Posted by Bruce Fox
VB Code:
Public Function Rand(ByVal Low As Long, _
ByVal High As Long) As Long
Dim N As Integer
High = 100
Low = 1
For N = 1 To 2
Rand = Int((High - Low + 1) * Rnd + Low)
Next
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.
-
Dec 26th, 2006, 07:15 AM
#13
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.
-
Dec 26th, 2006, 11:54 AM
#14
Re: Random Numbers -- Not so random?
 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,
-
Dec 26th, 2006, 12:39 PM
#15
Re: Random Numbers -- Not so random?
 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...
-
Mar 22nd, 2007, 04:43 PM
#16
Hyperactive Member
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.
-
Mar 22nd, 2007, 04:55 PM
#17
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).
-
Mar 22nd, 2007, 04:57 PM
#18
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.
-
Mar 23rd, 2007, 08:27 AM
#19
Hyperactive Member
Re: Random Numbers -- Not so random?
 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.
-
Mar 23rd, 2007, 09:12 AM
#20
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.
-
Mar 23rd, 2007, 09:12 AM
#21
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.
-
Mar 23rd, 2007, 09:31 AM
#22
Re: Random Numbers -- Not so random?
 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.
-
Mar 23rd, 2007, 10:42 AM
#23
Re: Random Numbers -- Not so random?
 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
-
Mar 23rd, 2007, 04:21 PM
#24
Hyperactive Member
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.
-
Mar 23rd, 2007, 04:58 PM
#25
Re: Random Numbers -- Not so random?
what are you trying to do with the random numbers?
-
Mar 23rd, 2007, 05:19 PM
#26
Hyperactive Member
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?
-
Mar 23rd, 2007, 05:25 PM
#27
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
-
Mar 23rd, 2007, 05:26 PM
#28
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)
-
Mar 23rd, 2007, 08:40 PM
#29
Hyperactive Member
Re: Random Numbers -- Not so random?
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
|