Package tripleplay.util
Class Randoms
java.lang.Object
tripleplay.util.Randoms
Provides utility routines to simplify obtaining randomized values.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
orfalse
with approximately even probability.boolean
getChance
(int n) Returns true approximately one inn
times.float
getFloat
(float high) Returns a pseudorandom, uniformly distributedfloat
value between0.0
(inclusive) and thehigh
(exclusive).float
getInRange
(float low, float high) Returns a pseudorandom, uniformly distributedfloat
value betweenlow
(inclusive) andhigh
(exclusive).int
getInRange
(int low, int high) Returns a pseudorandom, uniformly distributedint
value betweenlow
(inclusive) andhigh
(exclusive).int
getInt
(int high) Returns a pseudorandom, uniformly distributedint
value between0
(inclusive) andhigh
(exclusive).float
getNormal
(float mean, float dev) Returns a pseudorandom, normally distributedfloat
value around themean
with the standard deviationdev
.boolean
getProbability
(float p) Has a probabilityp
of returning true.<T> T
Pick a random element from the specified Iterable, or returnifEmpty
if it is empty.<T> T
Pick a random element from the specified Iterator, or returnifEmpty
if it is empty.<T> T
Pick a random key from the specified mapping of weight values, or returnifEmpty
if no mapping has a weight greater than0
.<T> T
Pluck (remove) a random element from the specified Iterable, or returnifEmpty
if it is empty.<T> void
Shuffle the specified list using our Random.static Randoms
A factory to create a new Randoms object.
-
Method Details
-
with
A factory to create a new Randoms object. -
getInt
public int getInt(int high) Returns a pseudorandom, uniformly distributedint
value between0
(inclusive) andhigh
(exclusive).- Parameters:
high
- the high value limiting the random number sought.- Throws:
IllegalArgumentException
- ifhigh
is not positive.
-
getInRange
public int getInRange(int low, int high) Returns a pseudorandom, uniformly distributedint
value betweenlow
(inclusive) andhigh
(exclusive).- Throws:
IllegalArgumentException
- ifhigh - low
is not positive.
-
getFloat
public float getFloat(float high) Returns a pseudorandom, uniformly distributedfloat
value between0.0
(inclusive) and thehigh
(exclusive).- Parameters:
high
- the high value limiting the random number sought.
-
getInRange
public float getInRange(float low, float high) Returns a pseudorandom, uniformly distributedfloat
value betweenlow
(inclusive) andhigh
(exclusive). -
getChance
public boolean getChance(int n) Returns true approximately one inn
times.- Throws:
IllegalArgumentException
- ifn
is not positive.
-
getProbability
public boolean getProbability(float p) Has a probabilityp
of returning true. -
getBoolean
public boolean getBoolean()Returnstrue
orfalse
with approximately even probability. -
getNormal
public float getNormal(float mean, float dev) Returns a pseudorandom, normally distributedfloat
value around themean
with the standard deviationdev
. -
shuffle
Shuffle the specified list using our Random. -
pick
Pick a random element from the specified Iterator, or returnifEmpty
if it is empty.Implementation note: because the total size of the Iterator is not known, the random number generator is queried after the second element and every element thereafter.
- Throws:
NullPointerException
- if the iterator is null.
-
pick
Pick a random element from the specified Iterable, or returnifEmpty
if it is empty.Implementation note: optimized implementations are used if the Iterable is a List or Collection. Otherwise, it behaves as if calling
pick(Iterator, Object)
with the Iterable's Iterator.- Throws:
NullPointerException
- if the iterable is null.
-
pick
Pick a random key from the specified mapping of weight values, or returnifEmpty
if no mapping has a weight greater than0
. Each weight value is evaluated as a double.Implementation note: a random number is generated for every entry with a non-zero weight after the first such entry.
- Throws:
NullPointerException
- if the map is null.IllegalArgumentException
- if any weight is less than 0.
-
pluck
Pluck (remove) a random element from the specified Iterable, or returnifEmpty
if it is empty.Implementation note: optimized implementations are used if the Iterable is a List or Collection. Otherwise, two Iterators are created from the Iterable and a random number is generated after the second element and all beyond.
- Throws:
NullPointerException
- if the iterable is null.UnsupportedOperationException
- if the iterable is unmodifiable or its Iterator does not supportIterator.remove()
.
-