Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

wierd Random

 
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.
 
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.
 
author
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:
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic