Skip to content

Instantly share code, notes, and snippets.

@steveash
steveash / PreshingRandomSequence.java
Last active February 17, 2023 23:05
Pseudo-random generator that doesn't repeat integers
import java.util.Random;
/**
* Generates a sequence of pseudo-random numbers such that they never repeat. Can handle sequence sizes up to
* length Int.MAX_VALUE. Will throw exceptions if you ask for more than that; maps the entire [0, Integer.MAX_VALUE]
* range onto itself but in a random order
* <link>http://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/</link>
*/
public class PreshingRandomSequence {
public static final int MAX_INT_PRIME = 2147483587;