Results 1 to 8 of 8

Thread: [RESOLVED] Question about the Random Class in Background Worker.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Thumbs up [RESOLVED] Question about the Random Class in Background Worker.

    Hello everyone,

    I'm trying to use the random class to display a random time and date every time and add them into a XML file. However when I use this code

    Code:
     Dim date_ As String = String.Format("{0:yyyy/MM/dd}", DateTime.Now)
                    Dim post_date As String = String.Format("{0:yyyy/MM/dd}", DateTime.Now)
    
    
                    Dim randomnumber As Integer
                    Dim RandomClass As New Random()
                    randomnumber = RandomClass.Next(4, 23)
    
                    Dim randomintforhours As Integer = randomnumber
    
    
                    randomnumber = RandomClass.Next(10, 59)
    
    
                    Dim randomintforminutes As Integer = randomnumber
                    randomnumber = RandomClass.Next(10, 59)
    
    
                    Dim randomintforseconds As Integer = randomnumber
    
                    date_ = date_ & " " & randomintforhours & ":" & randomintforminutes & ":" & randomintforseconds
                    date_gmt = date_gmt & " " & (randomintforhours - 4) & ":" & randomintforminutes & ":" & randomintforseconds
                    pdate = dayname & ", " & todaysdate & " " & (randomintforhours - 4) & ":" & randomintforminutes & ":" & randomintforseconds
    It does work, but when it's in the background worker and it's in the "For each item as string in ,,,.items" it doesn't work. I get duplicate dates. So I tried adding this below the code :

    Code:
    MsgBox(pdate)
    Then it does work, I just have to press the button several times, it does add them properly in the XML file also. So my question is how can I make this work properly but automatically? I'm very confused. I tried defining a previous number and do this -

    Code:
    If randomintforhours = prevhoursint then
    (new random class here)
    end if
    But it didn't work.

    If anyone knows how to fix this please do let me know, I've been struggling for over 6 hours on this... It totally wasn't worth the time. I'll pause with this for a moment and continue with something else.

    Anyway thanks for reading.
    Sincerly,
    Paramalodux.
    Last edited by Paramalodux; Jul 9th, 2016 at 04:19 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Question about the Random Class in Background Worker.

    You must be creating the Random class instance in the loop.

    You should only create one instance of the Random class, and use that wherever needed.
    If you create multiple instances of the random class in a very short period, then they can each create the same sequence of numbers since they can be seeded the same when initialized.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: Question about the Random Class in Background Worker.

    Quote Originally Posted by passel View Post
    You must be creating the Random class instance in the loop.

    You should only create one instance of the Random class, and use that wherever needed.
    If you create multiple instances of the random class in a very short period, then they can each create the same sequence of numbers since they can be seeded the same when initialized.
    Hi Passel,

    I really need this random class in a loop. I want it to generate a random date and time for each item. It does work when I add msgbox after the first code provided. But than it isn't automated. I want it fully automatic. Any idea how I can do that? Also I understand that duplicates may be generated. But I got 3 random integers. It generates random date/times for 43 items. There's always this sequence :

    (not dup)
    (dup)
    (dup)
    (dup)
    (not dup)
    (dup)
    (dup)
    (dup)

    and sometimes
    (dup)
    (dup)
    (not dup)
    (dup)
    (dup)

    I really need this fixed.

    Sincerly,
    Paramalodux.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Question about the Random Class in Background Worker.

    Quote Originally Posted by Paramalodux View Post
    Hi Passel,

    I really need this random class in a loop...
    That looks like what you are doing and I guess you do not like the results. Why not give passel's advice a go?
    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

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Question about the Random Class in Background Worker.

    Quote Originally Posted by Paramalodux View Post

    I really need this random class in a loop.
    But if declare the random class inside the loop then you will for sure get duplicates, declare the class at class scope (outside of sub/func) , for example,

    Code:
    Public Class Form1
    
        Private RandomClass As New Random
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For n = 1 To 10
                Dim date_ As String = String.Format("{0:yyyy/MM/dd}", DateTime.Now)
                Dim randomintforhours As Integer = RandomClass.Next(4, 23)
                Dim randomintforminutes As Integer = RandomClass.Next(10, 59)
                Dim randomintforseconds As Integer = RandomClass.Next(10, 59)
                date_ &= " " & randomintforhours & ":" & randomintforminutes & ":" & randomintforseconds
                Debug.WriteLine(date_)
            Next
        End Sub
    
    End Class
    Note its still possible that the class could return the same number but they shouldn't be back to back.

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

    Re: Question about the Random Class in Background Worker.

    I really need this random class in a loop
    No you don't. You might think you do, but passel is right. You get duplicate date because you are creating a new random class with the same seed every time. You need to create it once at the class level and reuse it.

    edit - it looks like there is a consensus here
    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
    38,988

    Re: Question about the Random Class in Background Worker.

    Since nobody explained it, here's what's going on: Random number generators don't generate random numbers. What they do is perform an equation where a portion of the result meets the criteria for being random. It's a calculation, though, and it starts with an initial number called a seed. Two random number generators that start with the same seed will generate identical sequences of numbers. The numbers in each sequence will meet the criteria for randomness relative to one another, but sequence1 number X will be the same as sequence2 number X for all X.

    So, when you create a Random object, it is created with a seed. That seed is the system time down to some level of precision. In VB6, it was the system time down to the second, but it may be down to the millisecond in .NET. In any case, it is the system time that is used as the seed. A loop can run VERY fast. In fact, it might be able to run thousands of times in a millisecond. So, if you are creating New Random objects inside the loop then they will all be starting with the exact same seed, even if .NET uses time down to the millisecond as a seed. If you are then just getting the first number in the sequence, then that first number will be identical for all of the Random objects that started with the same seed.

    To solve the problem, you do what passel said: Create ONE Random object outside of the loop, then call .Next on that one object over and over. After all, the numbers will be random relative to one another in the same sequence. They simply won't be random relative to the sequences produced by other Random objects created in the same loop unless the loop is slow enough that no two Random objects are created with the same seed...which is both unlikely and hard to control.

    One Random object is all you want or else you will have trouble like this.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: Question about the Random Class in Background Worker.

    Quote Originally Posted by Edgemeal View Post
    But if declare the random class inside the loop then you will for sure get duplicates, declare the class at class scope (outside of sub/func) , for example,

    Code:
    Public Class Form1
    
        Private RandomClass As New Random
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For n = 1 To 10
                Dim date_ As String = String.Format("{0:yyyy/MM/dd}", DateTime.Now)
                Dim randomintforhours As Integer = RandomClass.Next(4, 23)
                Dim randomintforminutes As Integer = RandomClass.Next(10, 59)
                Dim randomintforseconds As Integer = RandomClass.Next(10, 59)
                date_ &= " " & randomintforhours & ":" & randomintforminutes & ":" & randomintforseconds
                Debug.WriteLine(date_)
            Next
        End Sub
    
    End Class
    Note its still possible that the class could return the same number but they shouldn't be back to back.
    Thank you and Shaggy Hiker. I'm still new to backgroundworker and loops so I get confused sometimes. Anyway much appreciated .

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