r/AskComputerScience • u/fgennari • Sep 25 '25
Generate Random Sequence of Unique Integers From 0 to N
I'm not quite sure what sub to ask this on since it's somewhere between math and CS/programming. I would like a function that works as a generator which takes an integer from 0 to N and returns a random integer in the same range such that every value is returned exactly once. So, a 1:1 mapping from [0,N] => [0,N]. It doesn't have to be perfectly random, just mixed up to remove the correlation and avoid consecutive values. It's okay if there is some state preserved between calls.
N is an input and can be anything. If it was a power of two minus one, I could do lots of tricks with bitwise operations such as XORs.
Basically, I want something that works like the C++ standard library function std::shuffle(). But I want to be able to call this with N=1 billion without having to allocate an array of 1 billion sequential integers to start with. Runtime should scale with the number of calls to the function rather than N.
9
u/Avereniect Sep 25 '25
(a * x + b) % NwhereaandNare coprime.bis used to control which value 0 maps to.3
u/ghjm MSCS, CS Pro (20+) Sep 25 '25
Is it guaranteed that the number of calls you will make is small compared to the total range of integers being selected from?
2
u/apnorton Sep 25 '25
The naive approach that meets this requirement is to:
2
u/teraflop Sep 25 '25
You can do this using Hasty Pudding which is a block cipher whose block size can be an arbitrary number of bits:
1
u/jpgoldberg Sep 25 '25
I know that some statistical libraries offer sampling without replacement. But I don’t know whether the do so without an N-sized memory requirements.
If you are willing to tolerate a small amount of error, I suspect that using a Bloom filter might give you substantial memory savings. But I’ve never actually used one, so my suggestion is merely pointing in what might be a possible solution.
1
u/lmarcantonio Sep 25 '25
If you need to not have duplicates I guess you can't do better than *a bit* for each integer, since you need to remember what numbers you've already done... otherwise you could do a compromise and save the "last n" numbers picked and reroll on a duplicate.
I am not aware of a random generator which ensures the "all once and only once" property with different seeds (I guess you *could* design a specific generator but you would have to decide the order from the start... needing a full array for the series!)
1
u/elephant_ua Sep 25 '25
> But I want to be able to call this with N=1 billion without having to allocate an array of 1 billion sequential integers to start with.
But you are going to call it 1 billion times! You will need to store billion ingeres. Or you will cal it a couple of times? then repetitions shouldn't be an issue.
If you hit a repetition, you can move to next number. If you think you will hit long sequence of occupied numbers, i guess, you can create a sort of balanced tree which will store taken ranges, so finding next free number shouldn't take that long. Though i haven't think about this data structure that much.
3
u/pollrobots Sep 25 '25
"Numerical Recipes" has a section on "sub random" sequences (IIRC)
A Sobol sequence would fit the bill and they can be extremely easy to generate.
Any space filling curve could likely be adapted to this also.
0
u/These-Maintenance250 Sep 25 '25
import random
random.shuffle(my_list)