|
-
May 6th, 2005, 03:56 AM
#1
Thread Starter
Hyperactive Member
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
-
May 6th, 2005, 04:24 AM
#2
Re: Using Random() Question
MSDN has full documentation of Class Random()
I don't live here any more.
-
May 6th, 2005, 04:33 AM
#3
-
May 6th, 2005, 04:58 AM
#4
Re: Using Random() Question
 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.
-
May 6th, 2005, 06:26 AM
#5
Hyperactive Member
Re: Using Random() Question
 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 
-
May 6th, 2005, 06:46 AM
#6
PowerPoster
Re: Using Random() Question
 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.
-
May 6th, 2005, 08:41 AM
#7
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:
Dim r as random = new random()
...
messagebox.show(r.Next(0,10))
I don't live here any more.
-
May 6th, 2005, 08:58 AM
#8
Hyperactive Member
-
May 6th, 2005, 09:04 AM
#9
Re: Using Random() Question
 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.
-
May 6th, 2005, 09:06 AM
#10
PowerPoster
Re: Using Random() Question
 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:
Dim r as random = new random()
...
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.
-
May 6th, 2005, 10:36 AM
#11
Re: Using Random() Question
 Originally Posted by wossname
Search MSDN for "Random Class".
 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.
-
May 6th, 2005, 10:43 AM
#12
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
 
-
May 6th, 2005, 10:52 AM
#13
Re: Using Random() Question
 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?
-
May 6th, 2005, 10:59 AM
#14
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
 
-
May 6th, 2005, 11:08 AM
#15
PowerPoster
Re: Using Random() Question
 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.
-
May 6th, 2005, 11:18 AM
#16
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?
-
May 6th, 2005, 11:42 AM
#17
Re: Using Random() Question
 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
 
-
May 6th, 2005, 03:18 PM
#18
Hyperactive Member
Re: Using Random() Question
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|