r/AskComputerScience • • 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.

2 Upvotes

36 comments sorted by

View all comments

Show parent comment

1

u/fgennari Sep 25 '25

This is more complex than I was expecting. Let me just try the simple solution with a "nice" N to see if this approach is even an improvement over what I have. Or I can try the shuffle() to see if that's acceptable performance.

4

u/jpgoldberg Sep 25 '25 edited Sep 25 '25

Edit: if x is treated as a counter in the generator, say going from 0 up to but not including N, then the only condition that is needed is that the multiplier a and the modulus N be coprime.

If you make sure that a, b, and N are mutually co-prime you should be in good shape, but you will run a risk that it won’t run through all the the numbers before repeating.

You can check whether numbers are co-prime using std::gcd. Two numbers are co-prime if and only if their GCD is 1.

It's been decades since I've written any C, and I've never touched C++, so this might be very broken or non-idiomatic.

// returns first suitable multiplier above n/2
unsigned int multiplier(unsigned int n) {
    unsigned a;
    if (n < 3) {
        /* raise some error somehow */;
    }
    for (a = (n/2) + 1; gcd(a, n) != 1; ++a)
        ;
     return a;
}

That almost certainly doesn't need its own function, but I wanted to show it relatively self-contained

That assumes you are able to get a gcd function from somewhere. If not, there is

/* std::gcd supposedly exists in C++, so you won't need this */
unsigned int gcd(unsigned a, unsigned b) {
    unsigned tmp;
    while (b != 0) {
        /* a, b = b, a % b */
        tmp = b;
        b = a % b;
        a = tmp;
    }
    return a;
}

I haven't tested any of this.

1

u/jpgoldberg Sep 25 '25 edited Sep 25 '25

Actually it is much simpler than I thought. It turns out that I led you astray because I misunderstood what u/avereniect was suggesting. If x is used as a counter stepping from 0 unto N, then everything I said was irrelevant.

So all you need is for a to be coprime with N as u/averniect originally said.