Pure, serialisable randomness. The rng is plain data you can keep in application state, log, or send over the wire.
Two forms:
{:seed s :n counter} each value is a function of seed and counter (splitmix64),
so the same seed and call sequence always replay identically.
{:queue [6 1 4]} scripted dice for tests. roll-die pops the head and throws
when empty. shuffle leaves the collection unchanged and does
not consume the queue, so tests set deck order directly.
Every function returns the new rng alongside its result. Nothing is mutated.
Pure, serialisable randomness. The rng is plain data you can keep in application state, log, or send over the wire.
Two forms:
{:seed s :n counter} each value is a function of seed and counter (splitmix64),
so the same seed and call sequence always replay identically.
{:queue [6 1 4]} scripted dice for tests. `roll-die` pops the head and throws
when empty. `shuffle` leaves the collection unchanged and does
not consume the queue, so tests set deck order directly.
Every function returns the new rng alongside its result. Nothing is mutated.(choose rng coll)Returns [element rng'] for a uniformly chosen element of coll. Throws on an empty collection. A scripted rng pops the next queued value and uses it as a 0-based index into coll.
Returns [element rng'] for a uniformly chosen element of coll. Throws on an empty collection. A scripted rng pops the next queued value and uses it as a 0-based index into coll.
2^53 - 1, JavaScript's Number.MAX_SAFE_INTEGER.
Seeds this engine GENERATES stay at or below it so that they survive a JSON
round trip through a browser exactly (slice 31). mix above happily takes
any long and a hand-passed one still may be any long - this bounds only what
we hand out.
2^53 - 1, JavaScript's Number.MAX_SAFE_INTEGER. Seeds this engine GENERATES stay at or below it so that they survive a JSON round trip through a browser exactly (slice 31). `mix` above happily takes any long and a hand-passed one still may be any long - this bounds only what we hand out.
(next-int rng bound)Returns [k rng'] with k uniform in [0, bound). bound must be a positive long. A scripted rng pops and returns the next queued value as is.
Returns [k rng'] with k uniform in [0, bound). bound must be a positive long. A scripted rng pops and returns the next queued value as is.
(random-seed)A fresh, unpredictable seed - the same fallback state/new-game already uses when no :seed is given, extracted here so a caller that wants to know what seed it's using (rather than leave it implicit - see game_log.clj's start-game!) can generate one the same way.
Bounded to max-safe-seed, and that is a correctness fix rather than
tidiness (slice 31). This used to be a bare .nextLong, i.e. the full
64-bit range. JSON has one number type and JavaScript parses it as a double,
exact only to 2^53 - so a seed like 8256368015606065355 reached the browser
as 8256368015606065000, and a player who asked to replay it got a DIFFERENT
GAME. Silent, and it defeated the entire point of reporting the seed.
Found by a suspicious literal in a UI test, not by the test itself: the literal truncated identically, so the assertion passed while proving nothing.
2^53 is still 9 quadrillion games, and a seed's job is to be reproducible rather than unguessable.
A fresh, unpredictable seed - the same fallback state/new-game already uses when no :seed is given, extracted here so a caller that wants to *know* what seed it's using (rather than leave it implicit - see game_log.clj's start-game!) can generate one the same way. **Bounded to max-safe-seed, and that is a correctness fix rather than tidiness (slice 31).** This used to be a bare `.nextLong`, i.e. the full 64-bit range. JSON has one number type and JavaScript parses it as a double, exact only to 2^53 - so a seed like 8256368015606065355 reached the browser as 8256368015606065000, and a player who asked to replay it got a DIFFERENT GAME. Silent, and it defeated the entire point of reporting the seed. Found by a suspicious literal in a UI test, not by the test itself: the literal truncated identically, so the assertion passed while proving nothing. 2^53 is still 9 quadrillion games, and a seed's job is to be reproducible rather than unguessable.
(roll rng sides)Returns [value rng'] for one die with the given number of sides, value in 1..sides. A scripted rng pops and returns the next queued value as is.
Returns [value rng'] for one die with the given number of sides, value in 1..sides. A scripted rng pops and returns the next queued value as is.
(roll-dice rng n)Returns [values rng'] for n dice, rolled in order.
Returns [values rng'] for n dice, rolled in order.
(roll-die rng)Returns [value rng'] with value in 1..6.
Returns [value rng'] with value in 1..6.
(scripted values)A scripted rng that will roll the given values in order.
A scripted rng that will roll the given values in order.
(scripted? rng)(seeded seed)A seeded rng starting at counter 0.
A seeded rng starting at counter 0.
(shuffle rng coll)Returns [shuffled-vector rng'] (Fisher-Yates). A scripted rng returns the collection as a vector, unchanged, and is not consumed.
Returns [shuffled-vector rng'] (Fisher-Yates). A scripted rng returns the collection as a vector, unchanged, and is not consumed.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |