public class EditRNG extends StatefulRNG
RNG.CustomRandom
Modifier and Type | Field and Description |
---|---|
double |
rawLatest
The latest generated double, between 0.0 and 1.0, before changes for centrality and expected average.
|
DOUBLE_UNIT, FLOAT_UNIT, haveNextNextGaussian, nextNextGaussian, ran, random
Constructor and Description |
---|
EditRNG()
Constructs an EditRNG with a pseudo-random seed from Math.random().
|
EditRNG(long seed)
Construct a new EditRNG with the given seed.
|
EditRNG(long seed,
double expected)
Construct a new EditRNG with the given seed.
|
EditRNG(long seed,
double expected,
double centrality)
Construct a new EditRNG with the given seed.
|
EditRNG(RandomnessSource rs)
Construct a new EditRNG with the given seed.
|
EditRNG(RandomnessSource rs,
double expected)
Construct a new EditRNG with the given seed.
|
EditRNG(RandomnessSource rs,
double expected,
double centrality)
Construct a new EditRNG with the given seed.
|
EditRNG(String seed)
Construct a new EditRNG with the given seed.
|
EditRNG(String seed,
double expected)
Construct a new EditRNG with the given seed.
|
EditRNG(String seed,
double expected,
double centrality)
Construct a new EditRNG with the given seed.
|
Modifier and Type | Method and Description |
---|---|
Random |
asRandom() |
double |
between(double min,
double max)
Returns a value from a even distribution from min (inclusive) to max
(exclusive).
|
int |
between(int min,
int max)
Returns a value between min (inclusive) and max (exclusive).
|
long |
between(long min,
long max)
Returns a value between min (inclusive) and max (exclusive).
|
int |
betweenWeighted(int min,
int max,
int samples)
Returns the average of a number of randomly selected numbers from the
provided range, with min being inclusive and max being exclusive.
|
RNG |
copy()
Creates a copy of this StatefulRNG; it will generate the same random numbers, given the same calls in order, as
this StatefulRNG at the point copy() is called.
|
double |
getCentrality()
Gets the current centrality measure of this EditRNG.
|
double |
getExpected()
Gets the current expected average for this EditRNG.
|
<T> T |
getRandomElement(Collection<T> coll)
Returns a random element from the provided Collection, which should have predictable iteration order if you want
predictable behavior for identical RNG seeds, though it will get a random element just fine for any Collection
(just not predictably in all cases).
|
<T> T |
getRandomElement(List<T> list)
Returns a random element from the provided list.
|
short |
getRandomElement(ShortSet set)
Returns a random element from the provided ShortSet.
|
<T> T |
getRandomElement(T[] array)
Returns a random element from the provided array and maintains object
type.
|
RandomnessSource |
getRandomness() |
<T> Iterable<T> |
getRandomStartIterable(List<T> list)
Get an Iterable that starts at a random location in list and continues on through list in its current order.
|
double |
getRawLatest()
Gets the latest "un-biased" random double used to produce the most recent (potentially) biased random number
generated for another method in this class, such as nextDouble(), between(), or getRandomElement().
|
int |
next(int bits)
Get up to 32 bits (inclusive) of random state from the RandomnessSource.
|
boolean |
nextBoolean()
Get a random bit of state, interpreted as true or false with approximately equal likelihood.
|
double |
nextDouble()
Generate a random double, altered to try to match the expected average and centrality.
|
double |
nextDouble(double max)
This returns a random double between 0.0 (inclusive) and max (exclusive).
|
float |
nextFloat()
This returns a maximum of 0.99999994 because that is the largest Float
value that is less than 1.0f
|
double |
nextGaussian() |
int |
nextInt()
Returns a random integer, which may be positive or negative.
|
int |
nextInt(int bound)
Returns a random integer below the given bound, or 0 if the bound is 0 or
negative.
|
long |
nextLong()
Returns a random long, which may be positive or negative.
|
long |
nextLong(long bound)
Returns a random long below the given bound, or 0 if the bound is 0 or
negative.
|
<T> List<T> |
randomPortion(List<T> data,
int count)
Gets a random portion of a List and returns it as a new List.
|
<T> T[] |
randomPortion(T[] data,
T[] output)
Gets a random portion of data (an array), assigns that portion to output (an array) so that it fills as much as
it can, and then returns output.
|
int[] |
randomRange(int start,
int end,
int count)
Gets a random subrange of the non-negative ints from start (inclusive) to end (exclusive), using count elements.
|
<T> List<T> |
randomRotation(List<T> l)
Given a
List l, this selects a random element of l to be the first value in the returned list l2. |
void |
setCentrality(double centrality)
Gets the current centrality measure of this EditRNG.
|
void |
setExpected(double expected)
Sets the expected average for random doubles this produces, which must always be between 0.1 and 0.9, and will be
set to 0.5 if an invalid value is passed.
|
void |
setRandomness(RandomnessSource random) |
<T> ArrayList<T> |
shuffle(Collection<T> elements)
Shuffles a
Collection of T using the Fisher-Yates algorithm and returns an ArrayList of T. |
<T> T[] |
shuffle(T[] elements,
T[] dest)
Shuffle an array using the "inside-out" Fisher-Yates algorithm.
|
getState, setState, toString
public double rawLatest
public EditRNG()
public EditRNG(long seed)
seed
- used to seed the default RandomnessSource.public EditRNG(String seed)
seed
- used to seed the default RandomnessSource.public EditRNG(long seed, double expected)
seed
- used to seed the default RandomnessSource.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9public EditRNG(String seed, double expected)
seed
- used to seed the default RandomnessSource.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9public EditRNG(long seed, double expected, double centrality)
seed
- used to seed the default RandomnessSource.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9centrality
- if positive, makes results more likely to be near expected; if negative, the opposite. The
absolute value of centrality affects how centered results will be, with 0 having no effectpublic EditRNG(String seed, double expected, double centrality)
seed
- used to seed the default RandomnessSource.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9centrality
- if positive, makes results more likely to be near expected; if negative, the opposite. The
absolute value of centrality affects how centered results will be, with 0 having no effectpublic EditRNG(RandomnessSource rs)
rs
- the implementation used to generate random bits.public EditRNG(RandomnessSource rs, double expected)
rs
- the implementation used to generate random bits.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9public EditRNG(RandomnessSource rs, double expected, double centrality)
rs
- the implementation used to generate random bits.expected
- the expected average for random doubles, which will be capped between 0.1 and 0.9centrality
- if positive, makes results more likely to be near expected; if negative, the opposite. The
absolute value of centrality affects how centered results will be, with 0 having no effectpublic double nextDouble()
nextDouble
in class RNG
public double nextDouble(double max)
nextDouble
in class RNG
public double between(double min, double max)
public int between(int min, int max)
public long between(long min, long max)
RNG
public int betweenWeighted(int min, int max, int samples)
betweenWeighted
in class RNG
min
- the minimum bound on the return value (inclusive)max
- the maximum bound on the return value (exclusive)samples
- the number of samples to takepublic <T> T getRandomElement(T[] array)
getRandomElement
in class RNG
T
- the type of the returned objectarray
- the array to get an element frompublic <T> T getRandomElement(List<T> list)
getRandomElement
in class RNG
T
- the type of the returned objectlist
- the list to get an element frompublic short getRandomElement(ShortSet set)
Requires iterating through a random amount of the elements in set, so performance depends on the size of set but is likely to be decent. This is mostly meant for internal use, the same as ShortSet.
getRandomElement
in class RNG
set
- the ShortSet to get an element frompublic <T> T getRandomElement(Collection<T> coll)
Requires iterating through a random amount of coll's elements, so performance depends on the size of coll but is
likely to be decent, as long as iteration isn't unusually slow. This replaces getRandomElement(Queue)
,
since Queue implements Collection and the older Queue-using implementation was probably less efficient.
getRandomElement
in class RNG
T
- the type of the returned objectcoll
- the Collection to get an element from; remember, Map does not implement Collectionpublic double nextGaussian()
nextGaussian
in class RNG
public int nextInt(int bound)
public int nextInt()
public long nextLong()
public long nextLong(long bound)
public double getExpected()
public void setExpected(double expected)
expected
- the expected average to use, which should be 0.1 <= fairness < 0.9public double getCentrality()
public void setCentrality(double centrality)
centrality
- the new centrality measure to usepublic int next(int bits)
RNG
public <T> List<T> randomRotation(List<T> l)
RNG
List
l, this selects a random element of l to be the first value in the returned list l2. It
retains the order of elements in l after that random element and makes them follow the first element in l2, and
loops around to use elements from the start of l after it has placed the last element of l into l2.
randomRotation
in class RNG
T
- No restrictions on type. Changes to elements of the returned List will be reflected in the parameter.l
- A List
that will not be modified by this method. All elements of this parameter will be
shared with the returned List.l
that has been rotated so its first element has been randomly chosen
from all possible elements but order is retained. Will "loop around" to contain element 0 of l after the last
element of l, then element 1, etc.public <T> Iterable<T> getRandomStartIterable(List<T> list)
RNG
list
while you use the returned reference. And there'll be no
ConcurrentModificationException to detect such erroneous uses.getRandomStartIterable
in class RNG
list
- A list with a constant-time List.get(int)
method (otherwise performance degrades).Iterable
that iterates over list
but start at
a random index. If the chosen index is i
, the iterator
will return:
list[i]; list[i+1]; ...; list[list.length() - 1]; list[0]; list[i-1]
public <T> T[] shuffle(T[] elements, T[] dest)
RNG
shuffle
in class RNG
T
- can be any non-primitive type.elements
- an array of T; will not be modifieddest
- Where to put the shuffle. If it does not have the same length as elements
, this will use the
randomPortion method of this class to fill the smaller dest. MUST NOT be the same array as elements!dest
after modificationspublic <T> ArrayList<T> shuffle(Collection<T> elements)
RNG
Collection
of T using the Fisher-Yates algorithm and returns an ArrayList of T.public float nextFloat()
RNG
public boolean nextBoolean()
RNG
nextBoolean
in class RNG
public RandomnessSource getRandomness()
getRandomness
in class RNG
public void setRandomness(RandomnessSource random)
setRandomness
in class StatefulRNG
public <T> T[] randomPortion(T[] data, T[] output)
randomPortion
in class RNG
T
- can be any non-primitive type.data
- an array of T; will not be modified.output
- an array of T that will be overwritten; should always be instantiated with the portion lengthpublic <T> List<T> randomPortion(List<T> data, int count)
randomPortion
in class RNG
T
- can be any non-primitive typedata
- a List of T; will not be modified.count
- the non-negative number of elements to randomly take from datapublic int[] randomRange(int start, int end, int count)
randomRange
in class RNG
start
- the start of the range of numbers to potentially use (inclusive)end
- the end of the range of numbers to potentially use (exclusive)count
- the total number of elements to use; will be less if the range is smaller than countpublic double getRawLatest()
public RNG copy()
copy
in class StatefulRNG
Copyright © 2012–2016. All rights reserved.