Andrew Tomazos <firstname.lastname@example.org> wrote: > Given a positive integer N, I would like a function f(N,i) where i is > in [0,N) such that: > > f(N,0) = x0 > f(N,1) = x1 > f(N,2) = x2 > . > . > f(N,N-1) = x[N-1] > > where (x0, x1, x2, ..., x[N-1]) is a pseudo-random permutation of (0, 1, > 2, .., N-1). > > For example the output for N = 5 may be: > > f(5,0) = 3 > f(5,1) = 0 > f(5,2) = 2 > f(5,3) = 4 > f(5,4) = 1 > > Here (3,0,2,4,1) is a "pseudo-random" shuffle of (0,1,2,3,4) > > What is a closed form or algorithm for f? > > - N may be very large, so allocating an identity permutation array and > randomly shuffling it is out. Ideally calculating f(N,i) for some N > and i should take constant size and space. > > - If this is not possible than perhaps an easing constraint can be > added that a stateful solution that guarantees f(N,i) will be called > in ascending order of i values (ie f(N,0), f(N,1), f(N,2) and so on) > would be useful to. > > - f doesn't need to be "cryptographic strength", just sufficiently > random for statistical purposes.) > > - I note that maybe mutliplying by and moding by prime numbers (or > pure powers of primes) is somehow a useful tool as P^n where P is > prime forms a valid finite field. ? > > Thanks, > Andrew.
Generate an array of N random numbers. Generate a second array of numbers from 0 to N-1, in order. Sort the first array in ascending order while simultaneously re-arranging the second array to maintain its relation to the first array. Use the resulting second array to index your data.