Seeded Random Reproducible Draws

Draw the same sample sequence on every run by seeding Random explicitly, the property test fixtures and example data need to be stable.

JavaData Integrity

Code

snippet
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ReproducibleDraws {

    public static void main(String[] args) {
        // 固定种子:同一 JDK 版本上序列可复现,测试与样例数据用它
        List<Integer> first = draw(new Random(20261114), 5);
        List<Integer> second = draw(new Random(20261114), 5);

        System.out.println("first:  " + first);
        System.out.println("second: " + second);
        System.out.println("reproducible: " + first.equals(second));

        List<Integer> otherSeed = draw(new Random(42), 5);
        System.out.println("different seed differs: " + !otherSeed.equals(first));
    }

    // 从 1..49 抽 count 个不重复号码:边抽边从池里移除
    static List<Integer> draw(Random random, int count) {
        List<Integer> pool = new ArrayList<>();
        for (int n = 1; n <= 49; n++) {
            pool.add(n);
        }
        List<Integer> picked = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            picked.add(pool.remove(random.nextInt(pool.size())));
        }
        return picked;
    }
}

How the snippet works

Seeded Random Reproducible Draws keeps everything in the class ReproducibleDraws and the functions main and draw.

Repetition is confined to for (int n = 1; n <= 49; n++) — the rest of the file is straight-line.

Applying it

Running the file prints labeled results such as first:, second:, reproducible: — each operation’s effect is visible in the output.

Launch the file; main takes over and steps through draw.

Statements that matter

List<Integer> second = draw(new Random(20261114), 5);
picked.add(pool.remove(random.nextInt(pool.size())));
List<Integer> first = draw(new Random(20261114), 5);

Interactive Tools for This Topic

FAQ

What does Seeded Random Reproducible Draws print when run?
No setup needed: run the file and look for labeled lines like `first:`, `second:`, `reproducible:` in the output.