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
posted 14 years ago
The "correct" way to use the Random class is to instantiate it once (preferably using the noarg 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 noarg 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
No, tomorrow we rule the world! With this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
