diff --git a/DESIGN.md b/DESIGN.md index 2937574f4..e7db1a287 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -405,7 +405,7 @@ JDK defaults that a JDK update may change, and those are the ones module authors `WorldRandom` therefore does not implement `RandomGenerator`. Overriding every default would hold only until a JDK adds another one; a type of its own exposes nothing IrisApi does not define. The price is that it cannot be passed to JDK methods that take a `Random` or `RandomGenerator`, such as -`Collections.shuffle`. Its methods: +`Collections.shuffle`; `pick` and `shuffle` cover the list cases instead. Its methods: | Method | Definition | | --- | --- | @@ -418,13 +418,20 @@ price is that it cannot be passed to JDK methods that take a `Random` or `Random | `nextDouble(bound)`, `nextDouble(origin, bound)` | `origin + nextDouble() * (bound - origin)`, a result rounded up to `bound` replaced by the double below it; a width beyond `Double.MAX_VALUE` is halved first | | `nextGaussian()` | Marsaglia's polar method on `StrictMath.log` and `StrictMath.sqrt`; the pair's second value is discarded | | `chance(probability)` | `nextDouble() < probability` | +| `pick(items)` | `items.get(nextInt(items.size()))`; an empty list throws | +| `shuffle(items)` | Fisher-Yates in place: for `i` from `size - 1` down to `1`, swap `i` with `nextInt(i + 1)` | The state is one `long`, initially the seed. Methods without rejection draw exactly one value; how many values a method draws is part of the sequence. `StrictMath` is fixed bit for bit by the Java SE specification (fdlibm), so the gaussian needs no math of its own. Invalid bounds (`bound <= 0`, `origin >= bound`, NaN or infinite doubles, a probability outside `[0, 1]`) throw -`IllegalArgumentException`. The generator is not thread-safe: one stage uses its stream within one -invocation. +`IllegalArgumentException`, as does `pick` on an empty list. `pick` draws what `nextInt(size)` +draws. A shuffle of `n >= 2` elements makes `n - 1` `nextInt` calls; a shorter list draws nothing +and is not touched. `RandomAccess` lists are swapped through `List.set`; other lists are copied to +an array, shuffled there and written back through their `ListIterator`, like `Collections.shuffle`, +with the same result. A list that does not support `set` throws its `UnsupportedOperationException` +before any element changes. Weighted picking is left out until a stage needs it. The generator is +not thread-safe: one stage uses its stream within one invocation. `WorldRandomTest` pins every method to values computed by `src/test/reference/world_random.py`, an independent implementation of this table with Python integers and IEEE doubles. Python's `math.log` diff --git a/README.md b/README.md index 5172b2ffb..32849bdf1 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,9 @@ module may still condition its stages on it, as moss does on `iris:snow`. Stages and object generators draw from `com.volmit.random.WorldRandom`, an IrisApi type whose every method is defined and pinned by tests, so the same seed gives the same world on every JDK. It is SplitMix64 with `nextInt`, `nextLong` and `nextDouble` in plain, bounded and `origin, bound` forms, -plus `nextBoolean`, `nextFloat`, `nextGaussian` and `chance(probability)`. It does not implement -`java.util.random.RandomGenerator`, whose default methods the JDK may change. Bounded integers are +plus `nextBoolean`, `nextFloat`, `nextGaussian`, `chance(probability)`, `pick(list)` and +`shuffle(list)`. It does not implement `java.util.random.RandomGenerator`, whose default methods the +JDK may change, so use `pick` and `shuffle` instead of `Collections.shuffle`. Bounded integers are unbiased, `nextDouble` never returns its bound, and invalid bounds throw. It is not thread-safe: a stage uses its own stream within one invocation and must not share or keep it. diff --git a/src/main/java/com/volmit/random/WorldRandom.java b/src/main/java/com/volmit/random/WorldRandom.java index 0fd1d090c..17cbbfa78 100644 --- a/src/main/java/com/volmit/random/WorldRandom.java +++ b/src/main/java/com/volmit/random/WorldRandom.java @@ -1,5 +1,10 @@ package com.volmit.random; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; +import java.util.RandomAccess; + /** A random stream whose every value is defined here, so the same seed gives the same draws on * every machine and every JDK. World generation draws from it: stages through * {@link com.volmit.stage.StageContext#random()}, object generators through @@ -29,7 +34,10 @@ package com.volmit.random; * for ranges wider than the largest double); *
{@link RandomAccess} lists are swapped through {@link List#set}. Other lists, such as + * {@link java.util.LinkedList}, are copied to an array, shuffled there and written back through + * their {@link ListIterator}, like {@link Collections#shuffle}; the result is the same. A list that + * does not support {@code set} throws its {@link UnsupportedOperationException} before any element + * changes. + */ + public void shuffle(List> items) { + if (items.size() < 2) return; + if (items instanceof RandomAccess) { + fisherYates(items.size(), (i, j) -> Collections.swap(items, i, j)); + return; + } + shuffleThroughArray(items); + } + + private void shuffleThroughArray(List> items) { + Object[] copy = items.toArray(); + fisherYates(copy.length, (i, j) -> { + Object swapped = copy[i]; + copy[i] = copy[j]; + copy[j] = swapped; + }); + @SuppressWarnings("unchecked") + ListIterator