Link to home
Start Free TrialLog in
Avatar of nightshadz
nightshadzFlag for United States of America

asked on

System.Random(Int32) help

I'm having trouble understanding exactly how the seed works in System.Random(Int32).

Say I have the following code:

Dim random As Random = New Random(620757230)
intNext = random.Next(0, 3)

This is saying give me a random number between 0 and 2, right?  What exactly is the seed doing for this random number generator?
Avatar of kaufmed
kaufmed
Flag of United States of America image

What exactly is the seed doing for this random number generator?
If don't know, "random" numbers aren't truly random when talking about a computer. Such numbers are generated by some algorithm. This algorithm has a default "seed" (I don't know what it is for .NET). If you provide the same seed to two different Random objects, you should get the same value for each successive call to Next() on the objects. Providing different seeds changes the values you receive on subsequent calls to Next().
If you provide the same seed to two different Random objects, you should get the same value for each successive call to Next() on the objects.
This is dependent on the algorithm, however. There is the possibility that the algorithm used by the Random class combines the seed value with some internal, volatile value, like system time. In that case, you might not receive the same value from each Random object.
Avatar of nightshadz

ASKER

You have the option of not providing a seed. In my example I'm providing a seed but getting numbers between 0 and 2. How does providing a seed provide better randomness in this example. Assume the seed changes every time.
I believe when you call Next(), what's happening under the hood is that a (real) number between 0 and 1 is being generated. When you call the Next() function with bounds, that generated number is multiplied (and rounded in the case of int's) to give you a value between you bounds.

How does providing a seed provide better randomness in this example.
I don't think it really changes the "randomness" of the number you receive; rather it changes the sequence of numbers you receive. The numbers only *appear* random to you because you don't know the algorithm being used. If you did, you could "predict" the next number on subequent Next() calls. "Random" numbers are called pseudo-random because of their appearance of being random.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe I'm not asking my question correctly. When you provide different seed values but use random.next(0,3) what is happening here? How does the result differ with different seed values?
Maybe I'm not asking my question correctly. When you provide different seed values but use random.next(0,3) what is happening here? How does the result differ with different seed values?
Combine everything I said above, primarily the first part of http:#35703185 .

You're using a small domain, so I don't think you'll notice that much of a "randomness". Expand the domain and then start playing with the seed. You should see more of a difference in the range.
Thanks, I tested different seeds and got different lists like you said.  I understand better how this works now.