by TekMol · 5y ago · 0 comments
Say it is initialized with "size=5" then it might output:
3,5,2,1,4
Is there something like this?
It does not need much statistical resemblence to randomness. Just look kind of random to the eye. And the code should be short. A few lines of Javascript or so.
Maybe one approach might be to just loop through the sequence (1,2,3,4,5) and xor the number with some other number? Maybe with 0101010...?
I want the code to be complete and reproducible. So gimmeTheNumberAtPosition(x) will always return the same for the same x.
Also, FYShuffle is much more complex than I would like the algo to be.
Isn't the Lehmer code the thing that you want? Then you can specify your permutation with an integer and extract the individual digits from it repeatedly.
Fisher-Yates is one of the simplest
things out there IMO
Just using "$n xor $something" would be simpler. I am just not sure yet, what a good "$something" is.Here is a neat explanation:
https://preshing.com/20121224/how-to-generate-a-sequence-of-...
If you need even better properties (eg cryptographically secure) you can also look at PCG with k-dimensional equidistribution:
The reason it works is because the subgroup order generated by the element has to be divisible in the group order. There's only 1 and p. So unless it's trivial (i.e., the element 1), it's going to be p and thus it generates the whole group in p steps.
Thus, the easiest solution is to take a step-size that is prime and different from you size (that works for any size). Given the index of the current element, the next index would be `(current_index + step) % size`.
They are quite efficient for both HW and SW implementations.
Yes, when initialized with "size=5" the numbers in the output must be precisely 1,2,3,4,5 but in random looking order.
And I don't want to store the whole sequence. I want to say gimmeTheNumberAtPosition(x) and it return just that number.
[1]: http://pcgworkshop.com/archive/mawhorter2019anarchy.pdf [2]: https://github.com/solsword/anarchy
- xoring will only produce permutations that have period two, and every element will be part of a 2-cycle.
- if your n isn’t a power of two, it may produce numbers larger than n
The set of 2-cycles will have a lot of structure, too (for example, if your magic constant is even, all cycles will have either two even numbers or two odd numbers; if it is odd, all cycles will have an even and an odd number), but the above should already be bad enough to drop that idea.
if your n isn’t a power of two,
it may produce numbers larger
than n
This is the biggest problem.The others are not such big problems as I don't need strong resemblence to real randomness.
For example, it will still alternate odd and even numbers.
Also, an adversary can derive the xor key from a single sample, and predict the entire sequence from it.
There is no adversary. The use case is not about security.
int start = getRandomInt(seed) % N;
double k = N / 1.618;
k = k % 2 == 0 ? k - 1 : k; // k and N must be coprime
int nextElement = k * start % N;
start++;
This will jump wildly across your sequence and visit all elements (as k and N are chosen coprime). No need to store a full array. Is it statistically random? Of course not, e.g. you will never see two values close to each other right after another.