/*** *void srand(seed) - seed the random number generator * *Purpose: * Seeds the random number generator with the int given. Adapted from the * BASIC random number generator. * *Entry: * unsigned seed - seed to seed rand # generator with * *Exit: * None. * *Exceptions: * *******************************************************************************/
/*** *int rand() - returns a random number * *Purpose: * returns a pseudo-random number 0 through 32767. * *Entry: * None. * *Exit: * Returns a pseudo-random number 0 through 32767. * *Exceptions: * *******************************************************************************/
int __cdecl rand( void ) { _ptiddata ptd = _getptd();
/** * Creates a new random number generator. This constructor sets * the seed of the random number generator to a value very likely * to be distinct from any other invocation of this constructor. */ publicRandom() { this(seedUniquifier() ^ System.nanoTime()); }
/** * Creates a new random number generator using a single {@code long} seed. * The seed is the initial value of the internal state of the pseudorandom * number generator which is maintained by method {@link #next}. * * <p>The invocation {@code new Random(seed)} is equivalent to: * <pre> {@code * Random rnd = new Random(); * rnd.setSeed(seed);}</pre> * * @param seed the initial seed * @see #setSeed(long) */ publicRandom(long seed) { if (getClass() == Random.class) this.seed = newAtomicLong(initialScramble(seed)); else { // subclass might have overriden setSeed this.seed = newAtomicLong(); setSeed(seed); } }
/** * Returns the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence. The general * contract of {@code nextInt} is that one {@code int} value is * pseudorandomly generated and returned. All 2<sup>32</sup> possible * {@code int} values are produced with (approximately) equal probability. * * <p>The method {@code nextInt} is implemented by class {@code Random} * as if by: * <pre> {@code * public int nextInt() { * return next(32); * }}</pre> * * @return the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence */ publicintnextInt() { return next(32); }
/** * Generates the next pseudorandom number. Subclasses should * override this, as this is used by all other methods. * * <p>The general contract of {@code next} is that it returns an * {@code int} value and if the argument {@code bits} is between * {@code 1} and {@code 32} (inclusive), then that many low-order * bits of the returned value will be (approximately) independently * chosen bit values, each of which is (approximately) equally * likely to be {@code 0} or {@code 1}. The method {@code next} is * implemented by class {@code Random} by atomically updating the seed to * <pre>{@code (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)}</pre> * and returning * <pre>{@code (int)(seed >>> (48 - bits))}.</pre> * * This is a linear congruential pseudorandom number generator, as * defined by D. H. Lehmer and described by Donald E. Knuth in * <i>The Art of Computer Programming,</i> Volume 3: * <i>Seminumerical Algorithms</i>, section 3.2.1. * * @param bits random bits * @return the next pseudorandom value from this random number * generator's sequence * @since 1.1 */ protectedintnext(int bits) { long oldseed, nextseed; AtomicLongseed=this.seed; do { oldseed = seed.get(); nextseed = (oldseed * multiplier + addend) & mask; } while (!seed.compareAndSet(oldseed, nextseed)); return (int)(nextseed >>> (48 - bits)); }