-
Jan 4th, 2015, 07:58 PM
#1
Thread Starter
Addicted Member
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
Last edited by NinjaNic; Jan 4th, 2015 at 08:20 PM.
-
Jan 4th, 2015, 08:00 PM
#2
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.
My usual boring signature: Nothing
-
Jan 4th, 2015, 08:02 PM
#3
Thread Starter
Addicted Member
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)
-
Jan 4th, 2015, 08:07 PM
#4
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.
My usual boring signature: Nothing
-
Jan 4th, 2015, 08:11 PM
#5
Thread Starter
Addicted Member
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...)
-
Jan 4th, 2015, 08:36 PM
#6
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.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jan 4th, 2015, 09:04 PM
#7
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.
My usual boring signature: Nothing
-
Jan 5th, 2015, 04:52 AM
#8
Re: Random.Next - Not Very Random At All... (Solved)
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.
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Jan 5th, 2015, 07:03 AM
#9
Re: Random.Next - Not Very Random At All... (Solved)
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
Tags for this Thread
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
|