Andrew Lit

Ranch Hand

Posts: 135

Ellen Zhao

Ranch Hand

Posts: 581

posted 14 years ago

A well known algorithm for generating uniformly distributed random values is the linear congruential algorithm. This algorithm generates random integer values that range over all the possible positive and negative values of the integer type. It uses the formula

X(n+1) = ( m * X(n) + a ) mod d

Where the Xi are the generated random values, m is a constant multiplier, a is a constant addend and d is a constant divisor, which is usually a very big number and relatively prime to m. A seed value kicks off the sequence. The formula relies on the fact that integer arithmetic does not overflow but wraps around.

Does that help?

Regards,

Ellen

X(n+1) = ( m * X(n) + a ) mod d

Where the Xi are the generated random values, m is a constant multiplier, a is a constant addend and d is a constant divisor, which is usually a very big number and relatively prime to m. A seed value kicks off the sequence. The formula relies on the fact that integer arithmetic does not overflow but wraps around.

Does that help?

Regards,

Ellen

Ilja Preuss

author

Sheriff

Sheriff

Posts: 14112

posted 14 years ago

The "correct" way to use the Random class is to instantiate it once (preferably using the no-arg constructor) and reuse the instance:

Originally posted by Andrew Lit:

Hi,

i can't understand what is the reason for such a wierd output, first loop gives me different numbers but second gives me 11???

probably you could suggest me better way to generate numbers, thank you.

The "correct" way to use the Random class is to instantiate it once (preferably using the no-arg constructor) and reuse the instance:

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus