Package totalcross.util
Class Random
- java.lang.Object
-
- totalcross.util.Random
-
public class Random extends java.lang.ObjectThis 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'); ... }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description charbetween(char s, char e)Returns a random character between the given region.intbetween(int s, int e)Returns random integer in the given region.intgetSeed()Returns the first seed usedprotected intnext(int bits)Bits should be <= 31doublenextDouble()Returns a double floating-point value in the half-open range [0,1).intnextInt(int n)Returns an integer in the range [0,n).
-
-
-
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).
-
nextInt
public int nextInt(int n)
Returns an integer in the range [0,n). n must be > 0, else -1 is returned.- See Also:
between(char, char),between(int, int)
-
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 rangee- 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 rangee- the ending number in the range
-
getSeed
public int getSeed()
Returns the first seed used
-
-