Questions about Random Numbers in VB.NET

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

I'm writing some code which requires pseudo-random numbers.
Unfortunately, I've found random number generation in VB.NET to be
confusing. Can anyone help me with answers to these questions?

1. What is the purpose of the argument used in Rnd(Number)? From
experimentation, it seems that when Number is negative, it acts as a
seed, and when Number is positive, it does nothing. This seems like
strange behavior, and I wonder if there is an explanation. (The
documentation doesn't help much, saying only that "The value of Number
determines how Rnd generates a random number.")

2. The documentation seems to contain two contradictory assertions. It
says A: "For any given initial seed, the same number sequence is
generated because each successive call to the Rnd function uses the
previously generated number as a seed for the next number in the
sequence." However, it also says B: "To repeat sequences of random
numbers, call Rnd with a negative argument immediately before using
Randomize with a numeric argument. Using Randomize with the same value
for Number does not repeat the previous sequence." I don't see how
both A and B can be true. Experimentally, I can determine that B is
correct. Why, then, does the documentation contain statement A?

3. I've found that similar seed values produce similar sequences. For
instance, in the following code, X and Y tend to be identical for any
large value of N.

Rnd(-1)
Randomize(N)
X = Rnd()

Rnd(-1)
Randomize(N + 1)
Y = Rnd()

This may be okay for some applications, but it is a problem for mine.
Can anyone suggest a technique for seeding the random number generator
so that similar values do not produce similar sequences?


-TC
 
TC said:
I'm writing some code which requires pseudo-random numbers.
Unfortunately, I've found random number generation in VB.NET to be
confusing. Can anyone help me with answers to these questions?

1. What is the purpose of the argument used in Rnd(Number)? From
experimentation, it seems that when Number is negative, it acts as a
seed, and when Number is positive, it does nothing. This seems like
strange behavior, and I wonder if there is an explanation. (The
documentation doesn't help much, saying only that "The value of Number
determines how Rnd generates a random number.")
<snip>

I'll suggest that you look into the .NET Framework Random class:

http://msdn2.microsoft.com/en-us/library/system.random(vs.80).aspx

http://msdn2.microsoft.com/en-us/library/system.random_members(vs.80).aspx

The Next() method link should interest you.

http://msdn2.microsoft.com/en-us/library/system.random.next(VS.80).aspx
 
Back
Top