public class RNG extends AbstractRNG implements java.io.Serializable
IRNG
, which covers most of the API surface, but RNG implements
a decent amount of additional methods. You should consider if your code needs
these additional methods, and if not you should use IRNG as the type for when you
need some random number generator.
Includes methods for getting values between two numbers and for getting
random elements from a collection or array. There are methods to shuffle
a collection and to get a random ordering that can be applied as one shuffle
across multiple collections, such as via OrderedMap.reorder(int...)
,
ArrayTools.reorder(ArrayList, int...)
, and so on. You can construct
an RNG with all sorts of RandomnessSource implementations, and choosing them
is usually not a big concern because the default works very well. If you target
GWT, then it is suggested that you use GWTRNG
instead of RNG; both
implement IRNG
, which is enough for most usage across SquidLib, but
GWTRNG is optimized heavily for better performance on GWT, even returning long
values faster than implementations that natively do their math on longs. It has
worse performance on 64-bit PCs and mobile devices, but should also have better
performance on 32-bit PCs and mobile devices.
But if you do want advice on what RandomnessSource to use... DiverRNG
is the default, and is the fastest generator that passes most tests and can
produce all 64-bit values, and though relative to many of the others it has a
significantly shorter period (the amount of random numbers it will go through
before repeating the sequence), this almost never matters in games, and is
primarily relevant for massively-parallel scientific programs. DiverRNG has a
period of pow(2, 64)
as opposed to XoRoRNG
's
pow(2, 128) - 1
, or LongPeriodRNG
's pow(2, 1024) - 1
.
LightRNG
is a solid choice and a former default RandomnessSource;
additional features of LightRNG are exposed in MoonwalkRNG
and using
MoonwalkRNG is recommended if you need unusual features like skipping backwards
in a random number sequence, taking a result of a nextLong() call and reversing
it to get the state that produced it, or calculating the distance in number of
nextLong() calls between two results of nextLong() calls. LightRNG is a
StatefulRandomness, which lets it be used in StatefulRNG
, and so is
DiverRNG, but LightRNG is also a SkippingRandomness
, which means you can
leap forward or backwards in its sequence very efficiently (DiverRNG is not a
SkippingRandomness). ThrustAltRNG
provides similar qualities to LightRNG,
and is one of the fastest generators here, but can't produce all possible 64-bit
values (possibly some 32-bit values as well); it was the default at one point so
you may want to keep compatibility with some versions by specifying ThrustAltRNG.
The defaults have changed in the past as issues are found in various generators;
LightRNG has high quality all-around but is slower than the other defaults,
ThrustAltRNG can't produce all results, LinnormRNG passed tests in an earlier
version of the PractRand test suite but now fails in the current version, and now
the default is DiverRNG, which shares the known issue of LightRNG and LinnormRNG
that it can't produce the same result twice from nextLong()
until the
generator exhausts its period and repeats its output from the beginning.
For most cases, you should decide between DiverRNG, ThrustAltRNG, LightRNG,
LongPeriodRNG, MiniMover64RNG, and XoshiroStarPhi32RNG based on your priorities.
Some tasks are better solved by using a different class, usually GWTRNG
,
which can always be serialized on GWT to save its state easily and is usually the
fastest substitute for RNG on that platform. DiverRNG is the best if you want high
speed, very good quality of randomness, and expect to generate a reasonable quantity
of numbers for a game (less than 18446744073709551616 numbers) without any single
results being impossible. LightRNG is the second-best at the above criteria, but is
the best option if you need an RNG that can skip backwards or jump forwards without
incurring speed penalties. LongPeriodRNG is best if you for some reason need a massive
amount of random numbers (as in, ten quintillion would be far too little) or want to
split up such a large generator into unrelated subsequences. XoshiroStarPhi32RNG is
best if GWT is a possible target but you either need to generate more than
18446744073709551616 numbers (but less than 340282366920938463463374607431768211455
numbers) or you need to ensure that each 128-bit chunk of output is unique; if GWT is
a target but those specific needs don't matter, use GWTRNG. ThrustAltRNG and
MiniMover64RNG are both faster than DiverRNG usually (MiniMover64RNG is the fastest),
but because they are unable to generate some outputs, that may make them a poor choice
for some usage (ThrustAltRNG also has some bias toward specific numbers and produces
them more frequently, but not frequently enough to make it fail statistical tests, and
ThrustAltRNG can skip around in its output sequence like LightRNG).
There are many more RandomnessSource implementations! You might want significantly less
predictable random results, which IsaacRNG
can provide, along with a
large period. The quality of PermutedRNG
is also good, usually, and it
has a sound basis in PCG-Random, an involved library with many variants on its
RNGs.
There may be reasons why you would want a random number generator that uses 32-bit
math instead of the more common 64-bit math, but using a 32-bit int on desktop and
Android won't act the same as that same 32-bit int on GWT. Since GWT is stuck with
JavaScript's implementation of ints with doubles, overflow (which is needed for an
RNG) doesn't work with ints as expected, but does with GWT's implementation of longs.
If targeting GWT, you should probably consider GWTRNG
or SilkRNG
,
which would be used in place of this class and have a similar API. You can instead
choose a RandomnessSource that is efficient on GWT; Lathe32RNG
is
significantly faster at producing int values on GWT than any long-based generator,
and will produce the same results on GWT as on desktop or Android (not all 32-bit
generators do this). Starfish32RNG
goes one step further than Lathe32RNG at
an even distribution, and has better quality, but is slightly slower; it is also used
internally by GWTRNG. While Lathe32RNG can produce all ints over the course of its
period, it will produce some pairs of ints, or longs, more often than others and will
never produce some longs. Starfish32RNG will produce all longs but one.
Oriole32RNG
and XoshiroStarPhi32RNG
are also GWT-safe, but other
generators that were thought to be GWT-friendly are not. PintRNG
is a
GWT-unsafe generators with other uses, but should not be used on GWT. All other
RandomnessSources use longs, and so will be slower than the recommended Starfish32RNG
or Lathe32RNG on GWT, but probably are much faster on 64-bit JREs.
Modifier and Type | Class and Description |
---|---|
static class |
RNG.CustomRandom
A subclass of java.util.Random that uses a RandomnessSource supplied by the user instead of the default.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Random |
ran |
protected RandomnessSource |
random |
Constructor and Description |
---|
RNG()
Default constructor; uses
DiverRNG , which is of high quality, but low period (which rarely matters
for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers. |
RNG(java.lang.CharSequence seedString)
String-seeded constructor; uses a platform-independent hash of the String (it does not use String.hashCode,
instead using
CrossHash.hash64(CharSequence) ) as a seed for DiverRNG , which is of high quality,
but low period (which rarely matters for games), and has excellent speed, tiny state size, and natively generates
64-bit numbers. |
RNG(long seed)
Default constructor; uses
DiverRNG , which is of high quality, but low period (which rarely matters
for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers. |
RNG(RandomnessSource random)
Uses the provided source of randomness for all calculations.
|
Modifier and Type | Method and Description |
---|---|
long |
approximateBits(int bitCount)
Deprecated.
see the version in GreasedRegion,
GreasedRegion.approximateBits(RandomnessSource, int) |
java.util.Random |
asRandom() |
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 RNG; it will generate the same random numbers, given the same calls in order, as this RNG
at the point copy() is called.
|
boolean |
equals(java.lang.Object o) |
java.lang.Iterable<Coord> |
getRandomCellsIterable(int width,
int height,
int size)
Use that to get random cells in a rectangular map.
|
<T> T |
getRandomElement(T[] array)
Returns a random element from the provided array and maintains object
type.
|
RandomnessSource |
getRandomness() |
<T> java.lang.Iterable<T> |
getRandomStartIterable(java.util.List<T> list)
Get an Iterable that starts at a random location in list and continues on through list in its current order.
|
Coord[] |
getRandomUniqueCells(int startX,
int startY,
int width,
int height)
Gets an array of unique Coords, from (startX,startY) inclusive to (startX+width,startY+height) exclusive, in a
random order, with the array containing
width * height items. |
Coord[] |
getRandomUniqueCells(int startX,
int startY,
int width,
int height,
Coord[] dest)
Assigns to dest an array of unique Coords, from (startX,startY) inclusive to (startX+width,startY+height)
exclusive, in a random order, with dest after this is called containing the lesser of
width * height or
dest.length items. |
Coord[] |
getRandomUniqueCells(int startX,
int startY,
int width,
int height,
int size)
Gets an array of unique Coords, from (startX,startY) inclusive to (startX+width,startY+height) exclusive, in a
random order, with the array containing
Math.min(width * height, size) items. |
int |
hashCode() |
double |
maxDoubleOf(double bound,
int trials)
Gets the maximum random double between 0 and
bound generated out of trials generated numbers. |
float |
maxFloatOf(float bound,
int trials)
Gets the maximum random float between 0 and
bound generated out of trials generated numbers. |
int |
maxIntOf(int bound,
int trials)
Gets the maximum random int between 0 and
bound generated out of trials generated numbers. |
long |
maxLongOf(long bound,
int trials)
Gets the maximum random long between 0 and
bound generated out of trials generated numbers. |
double |
minDoubleOf(double bound,
int trials)
Gets the minimum random double between 0 and
bound generated out of trials generated numbers. |
float |
minFloatOf(float bound,
int trials)
Gets the minimum random float between 0 and
bound generated out of trials generated numbers. |
int |
minIntOf(int bound,
int trials)
Gets the minimum random int between 0 and
bound generated out of trials generated numbers. |
long |
minLongOf(long bound,
int trials)
Gets the minimum random long between 0 and
bound generated out of trials generated numbers. |
int |
next(int bits)
Get up to 32 bits (inclusive) of random output; the int this produces
will not require more than
bits bits to represent. |
boolean |
nextBoolean()
Get a random bit of state, interpreted as true or false with approximately equal likelihood.
|
void |
nextBytes(byte[] bytes)
Generates random bytes and places them into the given byte array, modifying it in-place.
|
Coord |
nextCoord(int width,
int height)
Gets a random Coord that has x between 0 (inclusive) and width (exclusive) and y between 0 (inclusive)
and height (exclusive).
|
float |
nextCurvedFloat()
Generates a random float with a curved distribution that centers on 0 (where it has a bias) and can (rarely)
approach -1f and 1f, but not go beyond those bounds.
|
double |
nextDouble()
Gets a random double between 0.0 inclusive and 1.0 exclusive.
|
double |
nextDoubleInclusive()
Gets a random double between 0.0 inclusive and 1.0 inclusive.
|
double |
nextDoubleInclusive(double outer)
This returns a random double between 0.0 (inclusive) and outer (inclusive).
|
float |
nextFloat()
Gets a random float between 0.0f inclusive and 1.0f exclusive.
|
float |
nextFloatInclusive()
Gets a random float between 0.0f inclusive and 1.0f inclusive.
|
float |
nextFloatInclusive(float outer)
This returns a random float between 0.0f (inclusive) and outer (inclusive).
|
int |
nextInt()
Get a random integer between Integer.MIN_VALUE to Integer.MAX_VALUE (both inclusive).
|
int |
nextIntHasty(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()
Get a random long between Long.MIN_VALUE to Long.MAX_VALUE (both inclusive).
|
long |
randomInterleave()
Deprecated.
See
GreasedRegion.randomInterleave(RandomnessSource) for where this will be moved |
int[] |
randomOrdering(int length)
Generates a random permutation of the range from 0 (inclusive) to length (exclusive).
|
int[] |
randomOrdering(int length,
int[] dest)
Generates a random permutation of the range from 0 (inclusive) to length (exclusive) and stores it in
the dest parameter, avoiding allocations.
|
<T> java.util.List<T> |
randomPortion(java.util.Collection<T> data,
int count)
Gets a random portion of a Collection 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> java.util.List<T> |
randomRotation(java.util.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 |
setRandomness(RandomnessSource random) |
<T> java.util.ArrayList<T> |
shuffle(java.util.Collection<T> elements)
Shuffles a
Collection of T using the Fisher-Yates algorithm and returns an ArrayList of T. |
<T> java.util.ArrayList<T> |
shuffle(java.util.Collection<T> elements,
java.util.ArrayList<T> buf)
Shuffles a
Collection of T using the Fisher-Yates algorithm. |
<T> T[] |
shuffle(T[] elements)
Shuffle an array using the Fisher-Yates algorithm and returns a shuffled copy.
|
<T> T[] |
shuffle(T[] elements,
T[] dest)
Shuffle an array using the Fisher-Yates algorithm.
|
<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.
|
java.io.Serializable |
toSerializable()
Returns this RNG in a way that can be deserialized even if only
IRNG 's methods can be called. |
java.lang.String |
toString() |
between, between, between, getRandomElement, getRandomElement, nextDouble, nextFloat, nextInt, nextLong, nextSignedInt, nextSignedLong, swap
protected RandomnessSource random
protected java.util.Random ran
public RNG()
DiverRNG
, which is of high quality, but low period (which rarely matters
for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers.
LightRNG
, ThrustAltRNG
,
LinnormRNG
, and MersenneTwister
. You can still use one of these by instantiating one of those
classes and passing it to RNG(RandomnessSource)
, which may be the best way to ensure the same results
across versions.public RNG(long seed)
DiverRNG
, which is of high quality, but low period (which rarely matters
for games), and has excellent speed, tiny state size, and natively generates 64-bit numbers. The seed can be
any long, including 0.seed
- any longpublic RNG(java.lang.CharSequence seedString)
CrossHash.hash64(CharSequence)
) as a seed for DiverRNG
, which is of high quality,
but low period (which rarely matters for games), and has excellent speed, tiny state size, and natively generates
64-bit numbers.public RNG(RandomnessSource random)
RNG()
as the constructor.random
- the source of pseudo-randomness, such as a LightRNG or LongPeriodRNG objectpublic java.util.Random asRandom()
public int betweenWeighted(int min, int max, int samples)
The inclusive and exclusive behavior is to match the behavior of the similar method that deals with floating point values.
This can be used to weight RNG calls to the average between min and max.
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 interface IRNG
getRandomElement
in class AbstractRNG
T
- the type of the returned objectarray
- the array to get an element frompublic <T> java.util.List<T> randomRotation(java.util.List<T> l)
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.
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> java.lang.Iterable<T> getRandomStartIterable(java.util.List<T> list)
list
while you use the returned reference. And there'll be no
ConcurrentModificationException to detect such erroneous uses.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)
shuffle
in interface IRNG
shuffle
in class AbstractRNG
T
- can be any non-primitive type.elements
- an array of T; will not be modifiedpublic <T> T[] shuffleInPlace(T[] elements)
shuffle(Object[], Object[])
.
shuffleInPlace
in interface IRNG
shuffleInPlace
in class AbstractRNG
T
- can be any non-primitive type.elements
- an array of T; will be modifiedpublic <T> T[] shuffle(T[] elements, T[] dest)
shuffle
in interface IRNG
shuffle
in class AbstractRNG
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.dest
after modificationspublic <T> java.util.ArrayList<T> shuffle(java.util.Collection<T> elements)
Collection
of T using the Fisher-Yates algorithm and returns an ArrayList of T.
shuffle
in interface IRNG
shuffle
in class AbstractRNG
T
- can be any non-primitive type.elements
- a Collection of T; will not be modifiedpublic <T> java.util.ArrayList<T> shuffle(java.util.Collection<T> elements, java.util.ArrayList<T> buf)
Collection
of T using the Fisher-Yates algorithm. The result
is allocated if buf
is null or if buf
isn't empty,
otherwise elements
is poured into buf
.
shuffle
in interface IRNG
shuffle
in class AbstractRNG
T
- can be any non-primitive type.elements
- a Collection of T; will not be modifiedbuf
- a buffer as an ArrayList that will be filled with the shuffled contents of elements;
if null or non-empty, a new ArrayList will be allocated and returnedpublic <T> java.util.List<T> shuffleInPlace(java.util.List<T> elements)
shuffle(Collection)
, which returns a List as well.
shuffleInPlace
in interface IRNG
shuffleInPlace
in class AbstractRNG
T
- can be any non-primitive type.elements
- a Collection of T; will be modifiedpublic int[] randomOrdering(int length)
randomOrdering
in interface IRNG
randomOrdering
in class AbstractRNG
length
- the size of the ordering to producepublic int[] randomOrdering(int length, int[] dest)
randomOrdering
in interface IRNG
randomOrdering
in class AbstractRNG
length
- the size of the ordering to producedest
- the destination array; will be modifiedpublic <T> java.util.List<T> randomPortion(java.util.Collection<T> data, int count)
T
- can be any non-primitive typedata
- a Collection 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)
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 float nextCurvedFloat()
Random.nextGaussian()
in that it
uses a curved distribution, but it is not the same. The distribution for the values is similar to Irwin-Hall, and
is frequently near 0 but not too-rarely near -1f or 1f. It cannot produce values greater than or equal to 1f, or
less than -1f, but it can produce -1f.public double nextDouble()
nextDouble
in interface IRNG
nextDouble
in class AbstractRNG
public float nextFloat()
nextFloat
in interface IRNG
nextFloat
in class AbstractRNG
public boolean nextBoolean()
rng.next(1)
, depending on the RandomnessSource implementation; the
default DiverRNG will behave fine, as will LightRNG and ThrustAltRNG (these all use similar algorithms), but
the normally-high-quality XoRoRNG will produce very predictable output with rng.next(1)
and very good
output with rng.nextBoolean()
. This is a known and considered flaw of Xoroshiro128+, the algorithm used
by XoRoRNG, and a large number of generators have lower quality on the least-significant bit than the most-
significant bit, where this method only checks the most-significant bit.nextBoolean
in interface IRNG
nextBoolean
in class AbstractRNG
public long nextLong()
nextLong
in interface IRNG
nextLong
in interface RandomnessSource
nextLong
in class AbstractRNG
public int nextIntHasty(int bound)
AbstractRNG.nextInt(int)
, but it is a little
faster than that method because this avoids special behavior for when bound is negative.
bound
- the outer bound (exclusive), can be negative or positivepublic void nextBytes(byte[] bytes)
Random.nextBytes(byte[])
.
bytes
- the byte array to fill with random bytes; cannot be null, will be modifiedjava.lang.NullPointerException
- if the byte array is nullpublic int nextInt()
nextInt
in interface IRNG
nextInt
in class AbstractRNG
public int next(int bits)
bits
bits to represent.next
in interface IRNG
next
in interface RandomnessSource
next
in class AbstractRNG
bits
- an int between 1 and 32, both inclusivepublic RandomnessSource getRandomness()
public void setRandomness(RandomnessSource random)
public RNG copy()
copy
in interface RandomnessSource
copy
in class AbstractRNG
@Deprecated public long approximateBits(int bitCount)
GreasedRegion.approximateBits(RandomnessSource, int)
bitCount
- an int, only considered if between 0 and 64, that is the average number of bits to set@Deprecated public long randomInterleave()
GreasedRegion.randomInterleave(RandomnessSource)
for where this will be movedpublic long minLongOf(long bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias toward zero, but all possible values are between 0, inclusive,
and bound, exclusive.bound
- the outer exclusive bound; may be negative or positivetrials
- how many numbers to generate and get the minimum ofpublic long maxLongOf(long bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias away from zero, but all possible values are between 0,
inclusive, and bound, exclusive.bound
- the outer exclusive bound; may be negative or positivetrials
- how many numbers to generate and get the maximum ofpublic int minIntOf(int bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias toward zero, but all possible values are between 0, inclusive,
and bound, exclusive.bound
- the outer exclusive bound; may be negative or positivetrials
- how many numbers to generate and get the minimum ofpublic int maxIntOf(int bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias away from zero, but all possible values are between 0,
inclusive, and bound, exclusive.bound
- the outer exclusive bound; may be negative or positivetrials
- how many numbers to generate and get the maximum ofpublic double minDoubleOf(double bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias toward zero, but all possible values are between 0, inclusive,
and bound, exclusive.bound
- the outer exclusive boundtrials
- how many numbers to generate and get the minimum ofpublic double maxDoubleOf(double bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias away from zero, but all possible values are between 0,
inclusive, and bound, exclusive.bound
- the outer exclusive boundtrials
- how many numbers to generate and get the maximum ofpublic float minFloatOf(float bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias toward zero, but all possible values are between 0, inclusive,
and bound, exclusive.bound
- the outer exclusive boundtrials
- how many numbers to generate and get the minimum ofpublic float maxFloatOf(float bound, int trials)
bound
generated out of trials
generated numbers.
Useful for when numbers should have a strong bias away from zero, but all possible values are between 0,
inclusive, and bound, exclusive.bound
- the outer exclusive boundtrials
- how many numbers to generate and get the maximum ofpublic java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public <T> T[] randomPortion(T[] data, T[] output)
nextLong()
, regardless of
the data being randomized.
LowStorageShuffler
, but without any object or array allocations.randomPortion
in interface IRNG
randomPortion
in class AbstractRNG
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 lengthMath.min(output.length, data.length)
unique items have been put into it from datapublic double nextDoubleInclusive()
public double nextDoubleInclusive(double outer)
outer
- the outer inclusive bound as a double; can be negative or positivepublic float nextFloatInclusive()
public float nextFloatInclusive(float outer)
outer
- the outer inclusive bound as a float; can be negative or positivepublic Coord nextCoord(int width, int height)
width
- the upper bound (exclusive) for x coordinatesheight
- the upper bound (exclusive) for y coordinatespublic java.lang.Iterable<Coord> getRandomCellsIterable(int width, int height, int size)
width
- The map's width (bounds the x-coordinate in returned coords).height
- The map's height (bounds the y-coordinate in returned coords).size
- The number of elements in the returned iterable or anything
negative for no bound (in which case the iterator is infinite, it's
up to you to bound your iteration).public Coord[] getRandomUniqueCells(int startX, int startY, int width, int height)
width * height
items.startX
- the inclusive starting x positionstartY
- the inclusive starting y positionwidth
- the width of the space to place Coords in, extending from startXheight
- the height of the space to place Coords in, extending from startYwidth * height
Coord items in random order, inside the given boundspublic Coord[] getRandomUniqueCells(int startX, int startY, int width, int height, int size)
Math.min(width * height, size)
items. If size is less than width
times height, then not all Coords in the space will be used.startX
- the inclusive starting x positionstartY
- the inclusive starting y positionwidth
- the width of the space to place Coords in, extending from startXheight
- the height of the space to place Coords in, extending from startYsize
- the size of the array to return; only matters if it is smaller than width * height
Math.min(width * height, size)
Coord items in random order, inside the given boundspublic Coord[] getRandomUniqueCells(int startX, int startY, int width, int height, Coord[] dest)
width * height
or
dest.length
items. This will not allocate a new array for dest, but will create a temporary int array for
handling the shuffle.startX
- the inclusive starting x positionstartY
- the inclusive starting y positionwidth
- the width of the space to place Coords in, extending from startXheight
- the height of the space to place Coords in, extending from startYdest
- a Coord array that will be modified to contain randomly-ordered Coords, but will not be resizedwidth * height
items assigned to random Coords inside the given boundspublic java.io.Serializable toSerializable()
IRNG
's methods can be called.toSerializable
in interface IRNG
toSerializable
in class AbstractRNG
Serializable
view of this RNG; always this
Copyright © Eben Howard 2012–2022. All rights reserved.