Package org.microproxy.starlark.stdlib
Class RandomModule
java.lang.Object
org.microproxy.starlark.stdlib.RandomModule
- All Implemented Interfaces:
org.microproxy.thirdparty.starlark.eval.StarlarkValue
public final class RandomModule
extends Object
implements org.microproxy.thirdparty.starlark.eval.StarlarkValue
@stdlib//jrandom: randomness from SecureRandom, for random.star and
uuid.star (starlarky's came from BouncyCastle's registrar). There is no seeding: every
value is unpredictable, like Python's random.SystemRandom.-
Method Summary
Modifier and TypeMethodDescriptionchoice(org.microproxy.thirdparty.starlark.eval.Sequence<?> seq) Selects an element uniformly from a non-empty sequence.org.microproxy.thirdparty.starlark.eval.StarlarkIntgetrandbits(org.microproxy.thirdparty.starlark.eval.StarlarkInt k) Produces a non-negative integer with at most the requested number of bits.org.microproxy.thirdparty.starlark.eval.StarlarkBytesrandbytes(org.microproxy.thirdparty.starlark.eval.StarlarkInt n) Produces random bytes using the same generator asurandom(org.microproxy.thirdparty.starlark.eval.StarlarkInt).org.microproxy.thirdparty.starlark.eval.StarlarkIntrandint(org.microproxy.thirdparty.starlark.eval.StarlarkInt a, org.microproxy.thirdparty.starlark.eval.StarlarkInt b) Chooses an integer uniformly between inclusive endpoints.org.microproxy.thirdparty.starlark.eval.StarlarkFloatrandom()Produces a uniformly distributed random fraction.org.microproxy.thirdparty.starlark.eval.StarlarkIntrandrange(org.microproxy.thirdparty.starlark.eval.StarlarkInt start, Object stop, org.microproxy.thirdparty.starlark.eval.StarlarkInt step) Chooses an integer uniformly from a stepped, stop-exclusive range.org.microproxy.thirdparty.starlark.eval.StarlarkList<?> sample(org.microproxy.thirdparty.starlark.eval.Sequence<?> population, org.microproxy.thirdparty.starlark.eval.StarlarkInt k, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) Samples sequence positions without replacement.voidshuffle(org.microproxy.thirdparty.starlark.eval.StarlarkList<?> x) Randomly permutes a mutable list in place.org.microproxy.thirdparty.starlark.eval.StarlarkFloatInterpolates between two numeric endpoints using a random fraction.org.microproxy.thirdparty.starlark.eval.StarlarkBytesurandom(org.microproxy.thirdparty.starlark.eval.StarlarkInt n) Produces cryptographically strong random bytes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.microproxy.thirdparty.starlark.eval.StarlarkValue
checkHashable, debugPrint, getStarlarkType, isAcyclic, isImmutable, repr, str, truth
-
Method Details
-
urandom
public org.microproxy.thirdparty.starlark.eval.StarlarkBytes urandom(org.microproxy.thirdparty.starlark.eval.StarlarkInt n) throws org.microproxy.thirdparty.starlark.eval.EvalException Produces cryptographically strong random bytes.- Parameters:
n- the number of bytes, from zero through 16 MiB- Returns:
- immutable random bytes
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the byte count is negative or exceeds the limit
-
randbytes
public org.microproxy.thirdparty.starlark.eval.StarlarkBytes randbytes(org.microproxy.thirdparty.starlark.eval.StarlarkInt n) throws org.microproxy.thirdparty.starlark.eval.EvalException Produces random bytes using the same generator asurandom(org.microproxy.thirdparty.starlark.eval.StarlarkInt).- Parameters:
n- the number of bytes, from zero through 16 MiB- Returns:
- immutable random bytes
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the byte count is negative or exceeds the limit
-
getrandbits
public org.microproxy.thirdparty.starlark.eval.StarlarkInt getrandbits(org.microproxy.thirdparty.starlark.eval.StarlarkInt k) throws org.microproxy.thirdparty.starlark.eval.EvalException Produces a non-negative integer with at most the requested number of bits.- Parameters:
k- the number of random bits, from zero through 134,217,728 bits- Returns:
- an integer sampled uniformly from
[0, 2**k) - Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the bit count is negative or exceeds the limit
-
randrange
public org.microproxy.thirdparty.starlark.eval.StarlarkInt randrange(org.microproxy.thirdparty.starlark.eval.StarlarkInt start, Object stop, org.microproxy.thirdparty.starlark.eval.StarlarkInt step) throws org.microproxy.thirdparty.starlark.eval.EvalException Chooses an integer uniformly from a stepped, stop-exclusive range.- Parameters:
start- the first value, or the stop value whenstopisNonestop- the exclusive endpoint, orNonefor a range starting at zerostep- the nonzero difference between successive values- Returns:
- a randomly selected range member
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the step is zero or the range is empty
-
randint
public org.microproxy.thirdparty.starlark.eval.StarlarkInt randint(org.microproxy.thirdparty.starlark.eval.StarlarkInt a, org.microproxy.thirdparty.starlark.eval.StarlarkInt b) throws org.microproxy.thirdparty.starlark.eval.EvalException Chooses an integer uniformly between inclusive endpoints.- Parameters:
a- the inclusive lower endpointb- the inclusive upper endpoint- Returns:
- a randomly selected integer from
athroughb - Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- ifbis less thana
-
random
public org.microproxy.thirdparty.starlark.eval.StarlarkFloat random()Produces a uniformly distributed random fraction.- Returns:
- a random float in
[0.0, 1.0)
-
uniform
public org.microproxy.thirdparty.starlark.eval.StarlarkFloat uniform(Object a, Object b) throws org.microproxy.thirdparty.starlark.eval.EvalException Interpolates between two numeric endpoints using a random fraction.- Parameters:
a- the first integer or float endpointb- the second integer or float endpoint- Returns:
- a randomly interpolated float
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if an endpoint is not a supported number
-
choice
public Object choice(org.microproxy.thirdparty.starlark.eval.Sequence<?> seq) throws org.microproxy.thirdparty.starlark.eval.EvalException Selects an element uniformly from a non-empty sequence.- Parameters:
seq- the sequence to choose from- Returns:
- one of the sequence elements
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the sequence is empty
-
shuffle
public void shuffle(org.microproxy.thirdparty.starlark.eval.StarlarkList<?> x) throws org.microproxy.thirdparty.starlark.eval.EvalException Randomly permutes a mutable list in place.- Parameters:
x- the list to reorder- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- if the list cannot be mutated
-
sample
public org.microproxy.thirdparty.starlark.eval.StarlarkList<?> sample(org.microproxy.thirdparty.starlark.eval.Sequence<?> population, org.microproxy.thirdparty.starlark.eval.StarlarkInt k, org.microproxy.thirdparty.starlark.eval.StarlarkThread thread) throws org.microproxy.thirdparty.starlark.eval.EvalException Samples sequence positions without replacement.- Parameters:
population- the source sequence; repeated values remain distinct positionsk- the number of positions to choosethread- the calling thread that owns the result list- Returns:
- a list of the selected elements in sampling order
- Throws:
org.microproxy.thirdparty.starlark.eval.EvalException- ifkis negative, too large or not representable as an int
-