Results 1 to 9 of 9

Thread: Random.Next - Not Very Random At All... (Solved)

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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:
    1. Public Class Form1
    2.  
    3.     Dim TestString As String = ""
    4.  
    5.     Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    6.         Timer1.Stop()
    7.         A()
    8.         B()
    9.         C()
    10.         D()
    11.         Label1.Text = TestString
    12.         TestString = ""
    13.     End Sub
    14.  
    15.     Private Sub A()
    16.         Dim R As New Random
    17.         Dim A As Int32 = CInt(R.Next(0, 100))
    18.         TestString += A.ToString & ", "
    19.     End Sub
    20.  
    21.     Private Sub B()
    22.         Dim R As New Random
    23.         Dim B As Int32 = CInt(R.Next(0, 100))
    24.         TestString += B.ToString & ", "
    25.     End Sub
    26.  
    27.     Private Sub C()
    28.         Dim R As New Random
    29.         Dim C As Int32 = CInt(R.Next(0, 100))
    30.         TestString += C.ToString & ", "
    31.     End Sub
    32.  
    33.     Private Sub D()
    34.         Dim R As New Random
    35.         Dim D As Int32 = CInt(R.Next(0, 100))
    36.         TestString += D.ToString
    37.     End Sub
    38.  
    39.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    40.         Timer1.Start()
    41.     End Sub
    42.  
    43. End Class
    Last edited by NinjaNic; Jan 4th, 2015 at 08:20 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,023

    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

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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)

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,023

    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

  5. #5

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    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...)

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,023

    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

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,562

    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.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,267

    Re: Random.Next - Not Very Random At All... (Solved)

    Quote Originally Posted by kebo View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width