public class BasicRandom32 extends java.util.Random implements RandomnessSource, java.io.Serializable
public class BasicRandom32 extends Random implements Serializable
without any other changes. Note, it does
extend java.util.Random for additional ease of integration, but doesn't use the slow synchronized
keyword
that Random's implementations do.
Modifier and Type | Field and Description |
---|---|
int |
state |
Constructor and Description |
---|
BasicRandom32() |
BasicRandom32(int seed) |
Modifier and Type | Method and Description |
---|---|
BasicRandom32 |
copy()
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the
copy, both will generate the same sequence of random numbers from the point copy() was called.
|
int |
next(int bits)
Gets an int with at most the specified amount of bits; don't confuse this with
nextInt(int) , which gets
a number between 0 and its int argument, where this draws from a different (larger) range of random results. |
int |
nextInt() |
int |
nextInt(int bound)
Returns a random non-negative integer between 0 (inclusive) and the given bound (exclusive),
or 0 if the bound is 0.
|
long |
nextLong()
Using this method, any algorithm that needs to efficiently generate more
than 32 bits of random data can interface with this randomness source.
|
long |
nextLong(long bound)
Exclusive on bound (which must be positive), with an inner bound of 0.
|
void |
setSeed(long seed)
Sets the seed using a long, by XORing the upper and lower halves of
seed and passing that to
setState(int) . |
void |
setState(int seed) |
<T> T[] |
shuffle(T[] elements)
Shuffle an array using the Fisher-Yates algorithm and returns a shuffled copy, freshly-allocated, without
modifying elements.
|
<T> T[] |
shuffleInPlace(T[] elements)
Shuffles an array in-place using the Fisher-Yates algorithm.
|
<T> T[] |
shuffleInPlace(T[] elements,
int length)
Shuffles an array in-place using the Fisher-Yates algorithm, affecting indices from 0 (inclusive) to length
(exclusive).
|
protected static <T> void |
swap(T[] arr,
int pos1,
int pos2)
Mutates the array arr by switching the contents at pos1 and pos2.
|
public BasicRandom32()
public BasicRandom32(int seed)
public void setState(int seed)
public final long nextLong()
RandomnessSource
nextLong
in interface RandomnessSource
nextLong
in class java.util.Random
public final int next(int bits)
nextInt(int)
, which gets
a number between 0 and its int argument, where this draws from a different (larger) range of random results. For
example, next(2)
can return any 2-bit int,
which is limited to 0, 1, 2, or 3. Note that if you request 0 bits, this can give you any int (32 bits).next
in interface RandomnessSource
next
in class java.util.Random
bits
- the number of bits to get, from 1 to 32public final int nextInt()
nextInt
in class java.util.Random
public int nextInt(int bound)
IRNG.nextSignedInt(int)
if you need a negative outer bound.
nextInt
in class java.util.Random
bound
- the outer bound (exclusive), should be positivepublic long nextLong(long bound)
nextLong()
.bound
- the outer exclusive bound; should be positive, otherwise this always returns 0Lpublic void setSeed(long seed)
seed
and passing that to
setState(int)
.setSeed
in class java.util.Random
seed
- the initial seedprotected static <T> void swap(T[] arr, int pos1, int pos2)
arr
- an array of T; must not be nullpos1
- an index into arr; must be at least 0 and no greater than arr.lengthpos2
- an index into arr; must be at least 0 and no greater than arr.lengthpublic <T> T[] shuffle(T[] elements)
elements
- an array of T; will not be modifiedpublic <T> T[] shuffleInPlace(T[] elements, int length)
random.shuffleInPlace(arr.items, arr.size)
. If you don't want the array modified, use
shuffle(Object[])
.
elements
- an array of T; will be modifiedpublic <T> T[] shuffleInPlace(T[] elements)
shuffle(Object[])
.
elements
- an array of T; will be modifiedpublic BasicRandom32 copy()
RandomnessSource
copy
in interface RandomnessSource
Copyright © Eben Howard 2012–2022. All rights reserved.