public class BasicRandom64 extends java.util.Random implements RandomnessSource, java.io.Serializable
public class BasicRandom64 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 |
---|---|
long |
state |
Constructor and Description |
---|
BasicRandom64()
Calls
seed(int) with a random int value (obtained using Math.random() ). |
BasicRandom64(int seed)
The recommended constructor, this guarantees the generator will have a period of at least 2 to the 20 (roughly
one million, but most if not all initial states will have much longer periods).
|
BasicRandom64(long seed)
Like
BasicRandom64(int) , this doesn't use the seed as-is, and instead uses it to get a valid state
(which is a long internally). |
Modifier and Type | Method and Description |
---|---|
BasicRandom64 |
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.
|
long |
getState() |
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 |
seed(int s)
Seeds the state using all bits of the given int
s . |
void |
setSeed(long seed)
Sets the seed using a long, passing its argument to
setState(long) . |
void |
setState(long 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> java.util.List<T> |
shuffleInPlace(java.util.List<T> elements)
Shuffles a Collection of T items in-place using the Fisher-Yates algorithm.
|
<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 BasicRandom64()
seed(int)
with a random int value (obtained using Math.random()
).public BasicRandom64(int seed)
seed
parameter.seed
- any int; will be used to get the actual state used in the generator (which is a long internally)public BasicRandom64(long seed)
BasicRandom64(int)
, this doesn't use the seed as-is, and instead uses it to get a valid state
(which is a long internally). If you want to duplicate the state of another BasicRandom64, get the existing state
either with the field state
or with getState()
(you could store the state and load it later
at this stage), then make some new BasicRandom64 (such as with new BasicRandom64(0);
) and call
setState(long)
with the previous state. You can also use copy()
.seed
- any long; will be mixed around and given to seed(int)
as an int, not used as-ispublic final void seed(int s)
s
. Between 33554432 and 4294967296 seeds are possible,
with the actual count probably much closer to 4294967296. This treats the top 25 bits of s
(moved to the
bottom, plus 1, to avoid a seed of 0) as the starting point and then generates the next state at most 127 times,
with each generated state taking less time than nextLong()
. Some of the starting states are entirely
possible to be within 127 steps of another starting state, so not all seeds are necessarily unique. This is not
guaranteed to put the generator on an optimal subcycle, but it is guaranteed that any subcycle will have a period
of at least 1048575.s
- all bits are used, none verbatim (0 is tolerated)public long getState()
public void setState(long 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)
setState(long)
. That method just sets the public
field state
to its argument currently, but it may do more to ensure cycle length in the future.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 <T> java.util.List<T> shuffleInPlace(java.util.List<T> elements)
T
- can be any non-primitive type.elements
- a List of T; will be modifiedpublic BasicRandom64 copy()
RandomnessSource
copy
in interface RandomnessSource
Copyright © Eben Howard 2012–2022. All rights reserved.