/*
 * NewRand.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Object Orientation'
 * section: `Example - Subclassing java.util.Random'
 *
 * The class java.util.Random implements a general random number generator.
 * Here we use the mechanism of inheritance to define our own random number
 * generator which is compatible with it and can be used in the same places.
 * We then use our class to create random BigIntegers. Our random number
 * generator is deliberately bad - it returns the same value every time, and
 * therefore we can tell it is being used when the BigInteger returned
 * is very repetative.
 */
import java.math.BigInteger;
import java.util.Random;

// Declare NewRandomGenerator to descend from Random
class NewRandomGenerator extends Random
{
  // Override nextBytes to return the
  // same byte over and over again.
  public void nextBytes(byte[] bytes)
  {
    for (int i=0; i<bytes.length; i++)
    {
      bytes[i] = 0x52;
    }
  }

  // Override nextInt to return the
  // same integer every time.
  public int nextInt()
  {
    return 52;
  }

  // Override nextLong to return the
  // same value every time.
  public long nextLong()
  {
    return 52;
  }

  // Override nextFloat to return the
  // same value every time.
  public float nextFloat()
  {
    return 0.634F;
  }

  // Override nextDouble to return the
  // same value every time.
  public double nextDouble()
  {
    return 0.634;
  }

  // Override nextGaussian to return the
  // same value every time.
  public double nextGaussian()
  {
    return 0.634;
  }
}


// Declare a class NewRand which demonstrates the use
// of NewRandomGenerator.
public class NewRand
{
  public static void main(String[] argv)
  {
    // Create new BigInteger using an object of
    // the class NewRandomGenerator
    // as the source of randomness.
    BigInteger b = new BigInteger(128,
                                  new NewRandomGenerator());

    // Print the integer. The lack of randomness is
    // obvious in base 16.
    System.out.println("Is this a random integer: " + b);
    System.out.print("In base 16, it clearly is not: ");
    System.out.println(b.toString(16));
  }
}

