S
S
Sergey Sokolov2015-07-29 10:28:57
Algorithms
Sergey Sokolov, 2015-07-29 10:28:57

How to set shuffling of an array with a relatively short number of parameters?

There are exactly N natural numbers in a row from 1 to N. They need to be shuffled, pseudo-randomly. So that this mixing is determined by a relatively small number of parameters (relative to the number of values).
It is clear that you can simply use an array of N numbers, randomly mixed.
For N = 2^i, I used a bit-order permutation - only i numbers from 0 to i needed to be stored to encode the shuffling.
Now I'm looking for a functional solution similar in "beauty" for an arbitrary N.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2015-07-29
@OLS

The fastest in your setting seems to me to use PRNG based on a
linear congruent method with the conditions:
1) m=2^L (where L is rounded up from log2(N+1))
2) a mod 4 = 1
3) c mod 2 = 1
(this will iterate over all numbers from 0 to m-1 exactly once)
and ignore all numbers greater than N
(a, c, X0 are the parameters that define the permutation).
Here is an example of work for N=21 (m=32)

A=25 C= 3 X0=22: 9;4;7;18;5;3;14;1;10;6;20;2;21;16;19;17;12;15;13;8;11;
A= 9 C= 9 X0=17: 2;5;15;16;10;3;4;13;1;18;11;12;21;6;9;19;20;14;7;8;17;
A=17 C=21 X0= 2: 17;11;16;5;10;4;19;13;18;7;12;1;6;21;15;20;9;14;3;8;2;
A=17 C=25 X0=12: 5;14;7;16;9;18;11;20;13;15;17;19;21;2;4;6;8;1;10;3;12;

There are methods with sorting, but they will require amounts of memory equal to N. I can describe if you are interested ...
By the way, what is the order and upper bound of N ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question