package org.jgap.util;


import org.jgap.*;

import java.util.Random;

/**
    Implementation of a randomX-compliant class based upon the
    built-in Java.util.Random generator.  Note that since the higher
    level result composition methods are different from those in the
    (undocumented) standard library, randomJava results will differ
    from those of the standard library for a given seed.

    

Designed and implemented in July 1996 by John Walker, kelvin@fourmilab.ch. */ public class randomJava extends randomX { private Random r; private int ibytes = 0; // Constructors /** Creates a new pseudorandom number generator, seeded from the current time. */ public randomJava() { /* Since Java's generator seeds itself from the current time when called with no arguments, we don't have to pass in a seed here. */ r = new Random(); } /** Creates a new pseudorandom number generator with a specified seed. @param seed initial seed for the generator */ public randomJava(long seed) { r = new Random(seed); } /** Set seed for generator. Subsequent values will be based on the given seed. @param seed seed for the generator */ public void setSeed(long seed) { super.setSeed(); // Notify parent seed has changed r.setSeed(seed); ibytes = 0; // Clear bytes in nextByte buffer } private int idat; /** Get next byte from generator. To minimise calls on the underlying Java generator, integers are generated and individual bytes returned on subsequent calls. A call on setSeed() discards any bytes in the buffer. @return the next byte from the generator. */ public byte nextByte() { byte d; if (ibytes <= 0) { idat = r.nextInt(); ibytes = 4; } d = (byte) idat; idat >>= 8; ibytes--; return d; } };

Ads help maintain this website.