Class Random


  • public class Random
    extends java.lang.Object
    This is a simple Linear Congruential Generator which produces random numbers in the range [0,2^31), derived from ran0 in Numerical Recipies. Note that ran0 isn't wonderfully random -- there are much better generators out there -- but it'll do the job, and it's fast and has low memory consumption.

    Here's a sample of how to use the Random class:

        Random r = new Random(0x1234); // the use of the same key is sometimes desirable
        for (...)
        {
           int nextRandomInt = r.between(0,10);
           char nextRandomChar = r.between('a','z');
           ...
        }
        
    • Constructor Summary

      Constructors 
      Constructor Description
      Random()
      Randomizes based on the current timestamp
      Random​(int _seed)
      The only reasonable seeds are between 0 and 2^31 inclusive; if you're negative, it'll get the absolute value of it.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char between​(char s, char e)
      Returns a random character between the given region.
      int between​(int s, int e)
      Returns random integer in the given region.
      int getSeed()
      Returns the first seed used
      protected int next​(int bits)
      Bits should be <= 31
      double nextDouble()
      Returns a double floating-point value in the half-open range [0,1).
      int nextInt​(int n)
      Returns an integer in the range [0,n).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Random

        public Random​(int _seed)
        The only reasonable seeds are between 0 and 2^31 inclusive; if you're negative, it'll get the absolute value of it.
      • Random

        public Random()
        Randomizes based on the current timestamp
    • Method Detail

      • next

        protected int next​(int bits)
        Bits should be <= 31
      • nextDouble

        public double nextDouble()
        Returns a double floating-point value in the half-open range [0,1).
      • between

        public char between​(char s,
                            char e)
        Returns a random character between the given region. Eg: between('a', 'e') returns a random character between 'a' and 'e'.
        Parameters:
        s - the start character in the range
        e - the ending character in the range
      • between

        public int between​(int s,
                           int e)
        Returns random integer in the given region. Eg: rand(1, 10) returns a random integer in the range of 1 to 10, inclusive. Note that if s == e, it will return in the range s and e+1.
        Parameters:
        s - the start number in the range
        e - the ending number in the range
      • getSeed

        public int getSeed()
        Returns the first seed used