Random.Next - Not Very Random At All... (Solved)
Hi!
To cut to the chase, when I want to create four random numbers they are all the same. (I do not want that.) Is this because I have 4 random numbers being generated almost at the same time; do they use the same random seed even though I declare R as NEW random?
By the way - this is just a test code but my real program does the same thing. Thanks for the help in advance!
vb Code:
Public Class Form1
Dim TestString As String = ""
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
A()
B()
C()
D()
Label1.Text = TestString
TestString = ""
End Sub
Private Sub A()
Dim R As New Random
Dim A As Int32 = CInt(R.Next(0, 100))
TestString += A.ToString & ", "
End Sub
Private Sub B()
Dim R As New Random
Dim B As Int32 = CInt(R.Next(0, 100))
TestString += B.ToString & ", "
End Sub
Private Sub C()
Dim R As New Random
Dim C As Int32 = CInt(R.Next(0, 100))
TestString += C.ToString & ", "
End Sub
Private Sub D()
Dim R As New Random
Dim D As Int32 = CInt(R.Next(0, 100))
TestString += D.ToString
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
End Class
Re: Random.Next - Not Very Random At All...
Yeah, it sure isn't if you use it that way. Every time you create a new Random object it is seeded with the current system time down to the second. If you re-seed a random number generator with the same seed, you will get the exact same sequence of numbers. Therefore, if you create multiple Random objects in a second, they will produces identical patterns.
What you need to do is create only ONE Random object, probably at form scope, and just use that one. You never need more than one anyways.
Re: Random.Next - Not Very Random At All...
Thank you for replying so fast, and using one random object makes it work! Thank you again! (+Rep)
Re: Random.Next - Not Very Random At All...
I remember learning this one the hard way, too. Random number generators are kind of interesting, because they can't exist. Figuring out a truly random pattern is quite hard, so what you get is pseudo-random number generators. They have to be seeded by something. Had that seed been the time down to the millisecond, things like this would have been less common and even harder to work out, because you'd still get non-randomness, they'd just be a whole lot less common.
By the way, you can use this feature for testing. If you want to generate the exact same sequence of "random" numbers for a test, you can supply a specific seed and get the exact same sequence over and over. I've never seen a need for this, but I recognize that it could be useful.
Re: Random.Next - Not Very Random At All...
One of the first programs I made was where you click a button and it moves to a 'random' point on the screen, but after a couple of times I realized it was using the same pattern. I think using Randomize() did the trick. But then, wouldn't the same random-random pattern appear on every computer that used it o_o (so confusing...)
Re: Random.Next - Not Very Random At All... (Solved)
I ran into this a while back. I was launching 4 instances of the same class at the same time (ok not at the SAME time, but apparently close enough to the same time to mess things a little). The each class has its own random instance and a system.timer set 100 mS. When the timer fired the class would generate a "random" voltage. I was perplexed because each class was generating "almost" the same sequence. It was close enough to say they were related, but I could see slight differences in each sequence. As it turned out, if I delayed each class instance by a second, the sequences were distinctively different.
Re: Random.Next - Not Very Random At All... (Solved)
Randomize also seeded with the system time. As long as you called it only one time, then it worked well. You ran into problems if you called Randomize in a tight loop, in which case you got the same sequence over and over. That's how I first encountered it, back in the days of VB6.
Re: Random.Next - Not Very Random At All... (Solved)
Quote:
Random number generators are kind of interesting, because they can't exist
They do exist but they're difficult because you can't generate them with logic. You need to harvest a bunch of enviromental information (time of day, core temperature, # of key strokes in the last week etc) and combine them to produce some form of seed. The problem is that any can be non-random under certain circumstances and therefore all of them potentially could be non-random. The answer to that is to use alot of them.
Quote:
If you want to generate the exact same sequence of "random" numbers for a test, you can supply a specific seed and get the exact same sequence over and over. I've never seen a need for this, but I recognize that it could be useful.
We use it alot for regression testing AI algorithms. They're inherently random so having a truly random sequence makes them effectively untestable. Having a predictable pseudo random sequence solves that problem. I've never seen a need for it in production code though.
Re: Random.Next - Not Very Random At All... (Solved)
Quote:
Originally Posted by
kebo
I ran into this a while back. I was launching 4 instances of the same class at the same time (ok not at the SAME time, but apparently close enough to the same time to mess things a little). The each class has its own random instance and a system.timer set 100 mS. When the timer fired the class would generate a "random" voltage. I was perplexed because each class was generating "almost" the same sequence. It was close enough to say they were related, but I could see slight differences in each sequence. As it turned out, if I delayed each class instance by a second, the sequences were distinctively different.
If I use a random in a class I use it like this:
Code:
Public Class someClass
Private Shared prng As New Random 'only one
Public Function someRand() As Integer
Return someClass.prng.Next(1, 1001)
End Function
End Class