• Post Reply Bookmark Topic Watch Topic
  • New Topic

wierd Random  RSS feed

 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ellen Zhao
Ranch Hand
Posts: 581
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it did.
thank you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!