Results 1 to 18 of 18

Thread: Using Random() Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Using Random() Question

    I haven't seen any real documentation on using the Random() method.. I am wondering, should I declare a single Random object at the application level for all clients attached to use or do you just declare a new Random() object in every method you need to generate a random number each time the method is ran? I want to avoid repeat sequences of numbers. The application is a multiuser server.

    Hinder

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Using Random() Question

    MSDN has full documentation of Class Random()
    I don't live here any more.

  3. #3
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Using Random() Question

    From what I can remember from my research into this subject, you can get away with using the same Random() object. When you call the Random.Next(int, int) method, the object automatically generates it's seed based on the system date and time to ensure that a random number is always generated.

    The old Rnd() function in VB6 required you to specify a seed which I believe could heighten the chances of duplication. I guess it depends on just how unique you want the numbers to be. The larger your range, the greater the chance of uniqueness.

    The other cool thing about the .NET class is that it can return random decimals too.

    I'll try and find the article that I read this in (although it might have been a Mike Gunderloy book)

    And if anyone wants to correct me, please do because I'm interested in this class too

    LMS
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Using Random() Question

    Quote Originally Posted by Lil Ms Squirrel
    From what I can remember from my research into this subject, you can get away with using the same Random() object. When you call the Random.Next(int, int) method, the object automatically generates it's seed based on the system date and time to ensure that a random number is always generated.

    The old Rnd() function in VB6 required you to specify a seed which I believe could heighten the chances of duplication. I guess it depends on just how unique you want the numbers to be. The larger your range, the greater the chance of uniqueness.

    The other cool thing about the .NET class is that it can return random decimals too.

    I'll try and find the article that I read this in (although it might have been a Mike Gunderloy book)

    And if anyone wants to correct me, please do because I'm interested in this class too

    LMS
    VB6 had the Randomize statement to avoid duplication.

    Anyway, using a single global Random object is MUCH faster and more efficient than using multiple objects as and when required.

    Once again this is mentioned in MSDN!!
    I don't live here any more.

  5. #5
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Using Random() Question

    Quote Originally Posted by wossname
    Once again this is mentioned in MSDN!!
    Wossy....perhaps you could provide a pointer to the MSDN article online because personally I found it to be quite unhelpful on the subject and there's no real tutorial or discussion in MSDN...
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Using Random() Question

    Quote Originally Posted by Lil Ms Squirrel
    Wossy....perhaps you could provide a pointer to the MSDN article online because personally I found it to be quite unhelpful on the subject and there's no real tutorial or discussion in MSDN...
    I agree. The problem with the MSDN approach, even when you have fathomed it out, is that it only produces a different set of numbers each time you use it if you do not turn your computer off in between. You have to use a seed based on something independent of the computer. EG use a permanent start date and use the elapsed seconds to NOW as a basis.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Using Random() Question

    Search MSDN for "Random Class".

    You don't have to seed the random class explicitly. It does it internally by default from the system timer.

    If you do reboot your machine then you will still get a new set of numbers each time. I don't nkow what you guys are doing to have these problems??!

    VB Code:
    1. Dim r as random = new random()
    2.  
    3. ...
    4.  
    5. messagebox.show(r.Next(0,10))
    I don't live here any more.

  8. #8
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Using Random() Question

    Gee Wossy, wrong side of the bed this morning? All we're getting at is that Random is one of those irritating MSDN topics that doesn't really explain much about how it works, just how to use it.

    If it was all as simple as reading MSDN, then maybe we should all start looking at RTFM.com
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Using Random() Question

    Quote Originally Posted by Lil Ms Squirrel
    Gee Wossy, wrong side of the bed this morning? All we're getting at is that Random is one of those irritating MSDN topics that doesn't really explain much about how it works, just how to use it.

    If it was all as simple as reading MSDN, then maybe we should all start looking at RTFM.com

    Hmm, maybe the online version of the MSDN page for Random is different (inferior) to the version I have installed on my PC based MSDN. I'l look into it. The PC one is quite detailed.
    I don't live here any more.

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Using Random() Question

    Quote Originally Posted by wossname
    Search MSDN for "Random Class".

    You don't have to seed the random class explicitly. It does it internally by default from the system timer.

    If you do reboot your machine then you will still get a new set of numbers each time. I don't nkow what you guys are doing to have these problems??!

    VB Code:
    1. Dim r as random = new random()
    2.  
    3. ...
    4.  
    5. messagebox.show(r.Next(0,10))
    You're right. (Provided you use ToString of course). I must have had a horrible coincidence when I tried it before. Honestly, I got the same sequence!!! But I got different sequences this time. Many thanks.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Using Random() Question

    Quote Originally Posted by wossname
    Search MSDN for "Random Class".
    Quote Originally Posted by Lil Ms Squirrel
    Wossy....perhaps you could provide a pointer to the MSDN article online because personally I found it to be quite unhelpful on the subject and there's no real tutorial or discussion in MSDN...
    Did it. Found This:
    http://msdn.microsoft.com/library/de...classtopic.asp

    Seems good.

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

    Re: Using Random() Question

    I must be using Wossy's version of documentation, because I found it quite clear. However, it clearly stated that once seeded, it generates a random sequence of numbers! The key there is that it may repeat. From my limited exposure to random number generators, I got the impression that they are periodic. That would suggest that they should be re-seeded at times or else two sequences of values, so long as there is a sufficient interval between them, will be identical. Normally, this is not an issue. However, I am working on porting a GA model into .NET, and will be using up to hundreds or thousands of numbers per second.

    What was unclear to me is how often I would need to re-seed. Should it be every few seconds, or can I, like the tides, only re-seed a couple times a day?
    My usual boring signature: Nothing

  13. #13
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Using Random() Question

    Quote Originally Posted by Shaggy Hiker
    What was unclear to me is how often I would need to re-seed.
    Perhaps a Random number generator could, upon a Reseed, deterimine a random number to determine the next time its to do the reseed?

    So, Upon a Reseed, it Determines A Random Integer X, whereby after X more calls to the random number generator, it'll reseed itself, and also determine a new X.

    X should be in a comforatble range. Not too small to appear silly, {ie... if X < 10, thats a bit overkill}, but not too large to run into repeated sequences {ie... if X > 1,000,000, perhaps it might have been repeating since Random Call 100,000 }

    So, How about 30,000 < X < 100000

    That should be sufficient?

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

    Re: Using Random() Question

    Excellent, we could wrap the Random class within a class HyperRandom that would have the same methods as Random, and would in fact call the Random class methods, but would also track the number of calls, and would re-seed the internal Random class based on a random interval determined by the Random class.
    My usual boring signature: Nothing

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Using Random() Question

    Quote Originally Posted by Shaggy Hiker
    Excellent, we could wrap the Random class within a class HyperRandom that would have the same methods as Random, and would in fact call the Random class methods, but would also track the number of calls, and would re-seed the internal Random class based on a random interval determined by the Random class.

    Are you in politics? Sounds very much like our local candidate in the general election yeaterday!!!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Using Random() Question

    Lets say we suspected that a Random Number Generator eventually always enters into a loop of repeating its outputed numbers.

    How would someone build a test to determine if that suspicion was valid or not, assuming the loop starts after a VERY LARGE NUMBER of returns?


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

    Re: Using Random() Question

    Quote Originally Posted by taxes
    Are you in politics? Sounds very much like our local candidate in the general election yeaterday!!!
    I guess I don't know what you are refering to, my post didn't seem political, so I assume you mean that I narrowly avoided comprehsibility. That wouldn't surprise me, because there were two many words in that post that had more than one meaning.

    Oddly enough, though, I was raised in politics. My mother was a politician, and I actually hung out at the statehouse sometimes. So, either way, you were right.

    As for testing the looping, I can think of one way to test. Make an array of 100 numbers. Instantiate a Random class, then fill the array with the first 100 numbers produced.

    Then perform a loop that gets the next number from random, and compares it against the first number in the array.

    If the number matches, get the next number, and compare it, etc. Now, it might be that the fifth number is the same as the first number, so two sequences could repeat for five numbers, and then you would have the current sequence still matching the original sequence, but you could also be starting a new sequence.

    Hmmm, you could actually have as many as 100 different tested sequences going at once (almost certainly there would be less). As each new number was generated, each tested sequence would have to be checked to see whether or not it matched the original sequence. If it did not, it would be deleted.....etc.

    An interesting problem, but slow.
    My usual boring signature: Nothing

  18. #18
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Using Random() Question

    Quote Originally Posted by NotLKH
    TVM NotLKH -- I was looking for an example myself but I usually look in local MSDN (which in this case was crap) and then find it to post as a reference on here for others to look at.

    However, copious amounts of wine down the pub at lunchtime to comiserate another miserable general election results meant that my ability to type decreased exponentially and I ended up searching google for "Random" and of course I got a whole bunch of Random articles back
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

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