• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Random from java.util

 
Rob van Oostveen
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,

I was thinking about using the Random class from java.util to generate unique numbers for my cookies.

I would be using the following method to generate the cookie.



I was wondering if this method is good enough though. The class description states the following:


An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.


Since I generate the cookie in the Data class which is instantiated only once, the Random class would also be instantiated once which in turn would (more or less) garantee unique generated numbers. True?

Regards,
Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic