|
-
Oct 30th, 2008, 08:30 AM
#1
Thread Starter
New Member
Disproving the Rnd()
Hey all,
I want to prove that the random number generator in VB is in fact NOT random.
I'm hoping for assisstance in how to write the following program.
When I click a button, I want to start an algorithm that will generate a random number 10000 times, and each time a new number is generated it will be compared to 10 "bins" i.e. 0 - 0.1 0.1-0.2 etc up to 1. Then the program will keep a running tally in the bins until the final run is finished At this point I should have a distribution of where all the random numbers are situated between 0 and 1 all written out in a text box in the same form.
Now i've written the code for it, but I cant get it to completely work, so I'm hoping that someone could give me a hand by showing how they would do this. The two main problems are that the tally wont remember the previous iteration and just resets them all to 0, and that just clicking the button only runs the iteration once, not 10000 times as I require.
Any help greatly appreciated for this beginner!
Indy
-
Oct 30th, 2008, 08:34 AM
#2
Re: Disproving the Rnd()
post your code. btw don't use rnd, use random.
-
Oct 30th, 2008, 08:36 AM
#3
Thread Starter
New Member
Re: Disproving the Rnd()
I'm at work at the moment so don't have access to my code, but I'll try and sort it out this evening for all to see.
Indy
-
Oct 30th, 2008, 08:44 AM
#4
Re: Disproving the Rnd()
what is not random? from the random class documentation
"Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981."
-
Oct 30th, 2008, 08:52 AM
#5
Thread Starter
New Member
Re: Disproving the Rnd()
I've just cobbled this together while at lunch. Syntax is probably a little ropey, but none the less.
The whole point of this is to prove what you mentioned above using simple code.
Dim i as integer
Dim num, bins() as single
for i = 1 to 10000
num = rnd()
if num =>0 <0.1 then
bins(0) = bins(0)+1
elseif num =>0.1<+0.19 then
bins(1) = bins(1)+1
elseif num =>0.2<+0.29 then
bins(2) = bins(2)+1
elseif num =>0.3<+0.39 then
bins(3) = bins(3)+1
elseif num =>0.4<+0.49 then
bins(4) = bins(4)+1
elseif num =>0.5<+0.59 then
bins(5) = bins(5)+1
elseif num =>0.6<+0.69 then
bins(6) = bins(6)+1
elseif num =>0.7<+0.79 then
bins(7) = bins(7)+1
elseif num =>0.8<+0.89 then
bins(8) = bins(8)+1
else
bins(9) = bins(9)+1
textbox1.text = "bin1="str(bins(0) +vbnewline_
& "bin2="str(bins(1)) +vbnewline_
& "bin3="str(bins(2)) +vbnewline_
& "bin4="str(bins(3)) +vbnewline_
& "bin5="str(bins(4)) +vbnewline_
& "bin6="str(bins(5)) +vbnewline_
& "bin7="str(bins(6)) +vbnewline_
& "bin8="str(bins(7)) +vbnewline_
& "bin9="str(bins(8)) +vbnewline_
& "bin10="str(bins(9)) +vbnewline_
end if
next i
end for
-
Oct 30th, 2008, 09:14 AM
#6
Re: Disproving the Rnd()
That code doesn't even compile, its mostly syntactically incorrect.
I still don't know what you are trying to prove here. I have used the random number generator many times in code, and it always produces number sequences that are totally random for conventional random number needing purposes.
-
Oct 30th, 2008, 09:30 AM
#7
Hyperactive Member
Re: Disproving the Rnd()
I give ya props for trying to prove it wrong (though I agree with the others that it will turn out to be random -- well at least they will 'appear' to be so mathematically). You will find in fact that they are not random as the same sequence will be generated for the same seed value. That aspect of it will be difficult to detect in your test.
At any rate I have a suggestion to clarify your code a bit. Rather than using if/then you could do something like this to get the bin number the random number belongs to:
Code:
Dim binNumber As Integer
binNumber = Math.Floor(yourRandomNumber * 10)
bins(binNumber) = bins(binNumber) + 1
-
Oct 30th, 2008, 09:31 AM
#8
Re: Disproving the Rnd()
Code:
Dim aRandom As New Random 'random number generator
Dim aNumber As Integer 'a palce to store a random number
Const howMany As Integer = 10
Dim hits(howMany - 1) As Integer 'accumulate hits
For ct As Integer = 1 To 100000 'generate some numbers
aNumber = aRandom.Next(0, howMany) 'get a random number
hits(aNumber) += 1 'count hits
Next
For ct As Integer = 0 To hits.Length - 1 'display distribution
Debug.WriteLine(ct.ToString.PadRight(5, " "c) & hits(ct).ToString)
Next
'0 9939
'1 9924
'2 9930
'3 9857
'4 10065
'5 10010
'6 9949
'7 10173
'8 10083
'9 10070
'
'stdev 96.3
'avgdev 80.2
'median 9979.5
'average 10000
-
Oct 30th, 2008, 09:34 AM
#9
Hyperactive Member
Re: Disproving the Rnd()
 Originally Posted by dbasnett
Code:
Dim aRandom As New Random 'random number generator
Dim aNumber As Integer 'a palce to store a random number
Const howMany As Integer = 10
Dim hits(howMany - 1) As Integer 'accumulate hits
For ct As Integer = 1 To 100000 'generate some numbers
aNumber = aRandom.Next(0, howMany) 'get a random number
hits(aNumber) += 1 'count hits
Next
For ct As Integer = 0 To hits.Length - 1 'display distribution
Debug.WriteLine(ct.ToString.PadRight(5, " "c) & hits(ct).ToString)
Next
'0 9939
'1 9924
'2 9930
'3 9857
'4 10065
'5 10010
'6 9949
'7 10173
'8 10083
'9 10070
'
'stdev 96.3
'avgdev 80.2
'median 9979.5
'average 10000
haha... your job just got harder indyhaan
-
Oct 30th, 2008, 09:35 AM
#10
Re: Disproving the Rnd()
"You will find in fact that they are not random as the same sequence will be generated for the same seed value. That aspect of it will be difficult to detect in your test."
yes, and? that was by design. if they wanted a new value from the same seed it would have been easy to do.
-
Oct 30th, 2008, 09:39 AM
#11
Hyperactive Member
Re: Disproving the Rnd()
 Originally Posted by dbasnett
"You will find in fact that they are not random as the same sequence will be generated for the same seed value. That aspect of it will be difficult to detect in your test."
yes, and? that was by design. if they wanted a new value from the same seed it would have been easy to do.
And, that's all. I was merely pointing out that fact to the original poster as he seemed to be unfamiliar with how the random number generator works. That the same sequence can be generated over and over again might be a bit odd to him.
-
Oct 30th, 2008, 11:52 AM
#12
Re: Disproving the Rnd()
I don't understand how you want to prove that the random number generator is not random...? If you want to show that the numbers are generated mathematically (and thus not random) then you can simply check your documentation, we know they are not random.
If you want to prove it without exploiting the fact that you already know them not to be random, you would probably have to try to reproduce the algorithm just from a list of generated pseudo-random numbers. I don't see how you're going to do that by the method you explained.
The only real random numbers can be generated by using some physical random occurrence (such as radioactive decay) but that is simply unpractical for most purposes as a pseudo-random number generator will do just fine.
-
Oct 30th, 2008, 12:37 PM
#13
-
Oct 30th, 2008, 12:42 PM
#14
Re: Disproving the Rnd()
I don't see how keeping a tally of the number of times a particular "random" number comes up helps to prove or disprove randomness. At best it only shows the spread of the numbers picked. dbassnet's results show a fair amount of even spread... which, given the number of iterations and the smaller range, seems appropriate. If given a sequence of three numbers picked by the computer, you can pick the next number the computer will pick... then you've essentially disproven random.... but if you can't.... then it's a case for random. Now, this is based on an undefined seed. If a specific seed is supplied, then all bets are off, because you will (or should) get the same "random" sequence each time.
-tg
-
Oct 30th, 2008, 01:37 PM
#15
Re: Disproving the Rnd()
This is a mathematical and philosophical problem - nothing is truly random, because given a set of quantities and data that properly define the state of a system at one point in time, we can predict the next state of the system - implying that the random numbers we get from a random number generator isn't actually truly random and neither is any random number generator that may be based on, for example, atmospheric noise (they claim true random numbers on their site, but as I said above - it's just a matter of predictability which is beyond our grasp).
There is no need to disprove that Rnd() or Random() isn't truly random because it is known in computer science that a random value function doesn't give true random numbers... it gives numbers that suit our purposes in our applications.
-
Oct 30th, 2008, 01:45 PM
#16
Hyperactive Member
Re: Disproving the Rnd()
nothing is truly random, because given a set of quantities and data that properly define the state of a system at one point in time, we can predict the next state of the system
the best we can do is assign probabilities to given outcomes. we cannot say with certainty that a particular event will occur. this is because we cannot know the state of a system exactly no matter what. damn that Heisenberg!
Last edited by wy125; Oct 30th, 2008 at 01:53 PM.
-
Oct 30th, 2008, 02:47 PM
#17
Re: Disproving the Rnd()
Actually almost everything is random (Heisenberg again). The only reason we can predict something is because the odds of happening this way are a lot greater than not happening.
Come to think of it, the random generator's output is one of the few things that are never random hehe.
-
Oct 30th, 2008, 04:32 PM
#18
Frenzied Member
Re: Disproving the Rnd()
Great link to the www.random.org website! The point is, since the numbers are generated by a clearly defined mathematical formula, they are most certainly not random. Their spread is random enough for most circumstances, but not for e.g. encryption. Have a look here: http://en.wikipedia.org/wiki/Random_number_generation
To the best of my knowledge, radioactive decay is completely random, so the site www.hotbits.com uses it to create truly random data.
Coming to the point, your code will show nothing but the near-equal distribution of the numbers, which is a characteristic of truly random number sequences as well. So you cannot use it to prove the extent of the randomness (or lack of it) in the Random class. There are randomness tests in existence which can show that with some certainty, but true randomness can never be shown with complete certainty. Once again, Google is your best friend. I'll try to find an article I read about some of them, detailing formulas and equations to use...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Oct 30th, 2008, 04:45 PM
#19
Re: Disproving the Rnd()
The randomness comes from a seed, which (as far as I have ever seen) any random number generating algo has. The seed being different is what causes the outcome to be different.
If there is some nice class, like the random class that lets you generate random numbers without explicitly definining a seed, then you can be sure its making its own using the system clock as its seed.
The OP has not responded back in a while, so we don't even know if there is an issue here or not.
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
|