Results 1 to 9 of 9

Thread: Random Numbers.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Random Numbers.

    Hey, I got New Thread working

    OK, now I got this code:

    VB Code:
    1. lblAddNo1.Text = Int(Rnd(9) * 100)

    Now the problem is, that everytime I open the form, the first numbers to appear are always the same, everytime the form is generated. How can I make it so that the numbers generated are TRULY random everytime the foprm is opened?
    I never know what to put in this section...



    So sue me... ... ... I'm just kidding...


    www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Random Numbers.

    VB Code:
    1. Dim objRandom As New System.Random( _
    2.   CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
    3.  
    4. Public Function GetRandomNumber( _
    5.   Optional ByVal Low As Integer = 1, _
    6.   Optional ByVal High As Integer = 100) As Integer
    7.   ' Returns a random number,
    8.   ' between the optional Low and High parameters
    9.   Return objRandom.Next(Low, High + 1)
    10. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Re: Random Numbers.

    What if i want to generate a seperate random number for many lables? It would be a lot of code if i have to use this for every label.
    I never know what to put in this section...



    So sue me... ... ... I'm just kidding...


    www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!

  4. #4
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Random Numbers.

    Ehm...you just need to place a method call to GetRandomNumber() for each label...
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    124

    Thumbs up Re: Random Numbers.

    I did that, but when it generates a random number, each label would get the same number assigned to it as long as my set range was the same for each label. Now I'm using this code using VB.NET's Random() function:

    VB Code:
    1. Dim RandomNo As Random = New Random
    2.  
    3. 'Then I put this on the label:
    4.  
    5. lblWhatever.Text = RandomNo.Next(100, 999)

    This does the job great.
    I never know what to put in this section...



    So sue me... ... ... I'm just kidding...


    www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!

  6. #6
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Random Numbers.

    I can't understand the difference between what you did and what mendhak suggested. BTW, I assume that mendhak's code should be placed in a shared class or in the form's code.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

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

    Re: Random Numbers.

    I can't understand what Mendhak was talking about on this one. He goes to the trouble of explicitly initializing the random object with the time tick. According to my reading of the documentation, that is the default behavior if the object is created without any arguments.

    GetRandomNumber is a thin wrapper of the Random.Next method. It does make a bit more sense (since both bounds are treated the same way), but adds overhead.

    Is this some thread safe concept at work?
    My usual boring signature: Nothing

  8. #8
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Random Numbers.

    Am I to understand from some of these answers that Randomize() is legacy. Because thats what I still use to reseed.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Random Numbers.

    Yes, it is legacy. And that is a good thing. Randomize worked fine, but there were some real oddities that you could run into if you were not careful. I made the mistake of putting it in a loop one time, and got identical series of numbers over and over again until the timer incremented so that the seed became different.

    Also, you must know the joy of figuring out how to get a random number between x and y. Probably have it memorized. Well, now you can forget it. The random object has functions to do things like that.
    My usual boring signature: Nothing

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