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.
Code
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
Passphrase GeneratorGenerate memorable multi-word passphrases from a fixed 150-word list with crypto randomness, separators, and capitalization options.Password GeneratorGenerate random passwords from selectable character classes using crypto.getRandomValues with rejection sampling and class guarantees.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.UUID v4 GeneratorGenerate cryptographically random UUIDs version 4 using the Web Crypto API. Produce single or batch UUIDs with optional uppercase formatting and copy-to-clipboard support.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.