Suppose I have a sequence of numbers: {n, n+1, n+2, ... n + m}
Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3,...m} will spit out the original set in a random (or at least pseudo random) order.
For example assume my sequence is {10, 11, 12, 13, 14, 15, 16, 17}
f(1) could yield 14 f(2) could yield 17 f(3) could yield 13 f(4) could yield 10 f(5) could yield 16 f(6) could yield 15 f(7) could yield 11 f(8) could yield 12
At one point in the past a co-worker showed me a mathematical algorithm that was able to do this, but I have since forgotten almost everything about it other than it existed. I remember that you had to have the sequence in advance, and generate some constants from the sequence which were used in the function. And for those wondering, I have sadly lost contact with that co-worker.
This question's answers looks close to what I want, but I am not sure if the answers allow me to constrain the output to a specific sequence ahead of time.
Edit:
To clarify a little more I don't want to store the original sequence, or the shuffled sequence. I want to generate a function f() from the original sequence.
What is frustrating is that I have seen this, I just cannot remember enough about it to find it again with google.
The Fisher-Yates algorithm is great for permuting or shuffling a deck, but it is not what I am looking for.
Pseudo Random Number Generator(PRNG) refers to an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers. A PRNG starts from an arbitrary starting state using a seed state.
Instead they rely on algorithms to mimic the selection of a value to approximate true randomness. Pseudo random number generators work with the user setting the distribution, or scope from which the random number is selected (e.g. lowest to highest), and the number is instantly presented.
A pseudo-random number generator (PRNG) is a program written for, and used in, probability and statistics applications when large quantities of random digits are needed. Most of these programs produce endless strings of single-digit numbers, usually in base 10, known as the decimal system.
Pseudo-Random Numbers. A pseudo-random number generator is an algorithm which produces a sequence of numbers whose properties approximate the properties of sequences of random numbers. The C language provides such a generator in its library.
There is a simple function that generates a permutation of [0..m-1]
for a given m
. Just pick a number k
, relatively prime to m
and let f(i)=(k*i) mod m
. This always generates a permutation (no repeats on 0<=i<m
). It works better if k
is larger than m
.
For example, m=20, let k=137 (Python code, %
means modulo):
>>> [(137*i) % 20 for i in range(20)]
[0, 17, 14, 11, 8, 5, 2, 19, 16, 13, 10, 7, 4, 1, 18, 15, 12, 9, 6, 3]
This is a very simple PRNG, no guarantees about its statistical properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With