Random ints Streams For Bounded Draws

Draw bounded samples with Random.ints — origin inclusive, bound exclusive — and re-seeding with the same value replays the exact same sequence every run.

JavaData Integrity

Code

snippet
import java.util.Random;

public class DiceSample {

    public static void main(String[] args) {
        Random dice = new Random(42); // 固定种子:同种子同调用序列 = 相同结果

        // ints(streamSize, origin, bound):origin 含、bound 不含
        System.out.print("draws: ");
        dice.ints(5, 1, 7).forEach(d -> System.out.print(d + " "));
        System.out.println();

        // 重新播种同一序列:重放得到完全相同的 5 个数
        Random replay = new Random(42);
        System.out.println("replay sum : " + replay.ints(5, 1, 7).sum());

        // 汇总统计直接挂在同一条流上
        var stats = new Random(42).ints(5, 1, 7).summaryStatistics();
        System.out.println("stats      : " + stats);
    }
}

The shape of the program

Structurally, expect the class DiceSample and the function main and nothing else.

Points of care

  • The single prerequisite is the language toolchain itself.

Applying it

Entry signature: public static void main(String[] args)

Steps print with identifying tags: draws:, replay sum :, stats :.

Statements that matter

var stats = new Random(42).ints(5, 1, 7).summaryStatistics();
Random dice = new Random(42); // 固定种子:同种子同调用序列 = 相同结果
// ints(streamSize, origin, bound):origin 含、bound 不含
System.out.print("draws: ");
Random replay = new Random(42);

Interactive Tools for This Topic

FAQ

What does Random ints Streams For Bounded Draws print when run?
Invoke the file as-is — the `draws:`, `replay sum :`, `stats :` prefixes identify which step printed each line.