Earlier quoted context omitted.
Generating random permutations is quite a different problem from creating random numbers. The standard algo for random permutations is the Fisher-Yates Shuffle. https://en.wikipedia.org/wiki/Fisher–Yates_shuffle ETA: As you don't want to store the permutation, you might want to pick a number randomly from 1 to n!, and then generate the permutation on the fly up to the desired element, using the techniques outlined he…
Storing a number of the order of magnitude of n! will take the same amount of memory than storing a permutation of n elements, so what's the point ?
Evolution of Random Number Generators
41–50 of 56 posts
Re: Evolution of Random Number Generators
#42Earlier quoted context omitted.
Probably looking for this: https://en.m.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
This seems to use an external random number generator. 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.
Re: Evolution of Random Number Generators
#43Earlier quoted context omitted.
MT is pretty slow. There are fast variants (SFMT), but why use that when can have much better and much faster rngs? https://rurban.github.io/dieharder/QUALITY.html
Very useful! Thanks! Aren't there AES related intrinsics for some architectures that can be really fast for rng? How do they compare?
Re: Evolution of Random Number Generators
#44I am looking for a simple random number generator that fills a given amount of slots. 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) a…
If you want a simple pseudo random sequence of N that just looks random (pseudocode): 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. y…
Re: Evolution of Random Number Generators
#45Earlier quoted context omitted.
Ah yes, you are right. In that sense, it is a random permutation of the numbers from 1 to max. 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.
for JS you can probably just do `[...Array(n).keys()].sort(()=>Math.random() - 0.5)` ?
https://www.robweir.com/blog/2010/02/microsoft-random-browse...
The Fisher–Yates shuffle is the right way to shuffle an array in an unbiased way.
Re: Evolution of Random Number Generators
#46Earlier quoted context omitted.
What I mean is, in your example, do you need to have precisely the numbers 1, 2, 3, 4, and 5, but in any order? Or is the constraint something else?
Ah yes, you are right. In that sense, it is a random permutation of the numbers from 1 to max. 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.
Re: Evolution of Random Number Generators
#47I am looking for a simple random number generator that fills a given amount of slots. 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) a…
If size is a prime, repeated multiplication on a generator element (any element) will work if you compute `mod p`. But it's not going to have nice random properties for the most part. It's just a "permutation". 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 thu…
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`.
Re: Evolution of Random Number Generators
#48Earlier quoted context omitted.
I think both the PCG family and the xoroshiro family are faster still. https://prng.di.unimi.it https://www.pcg-random.org ETA: https://github.com/lemire/testingRNG
Does anyone know what's the current consensus about PCG vs xoroshiro. I remember that the xoroshiro author had several complaints about PCG but I don't know ifhe is right or if he was just being salty.
[1] https://www.pcg-random.org/posts/xoshiro-repeat-flaws.html
Re: Evolution of Random Number Generators
#49Earlier quoted context omitted.
This seems to use an external random number generator. 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.
Fisher-Yates is one of the simplest things out there IMO. 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.Re: Evolution of Random Number Generators
#50I am looking for a simple random number generator that fills a given amount of slots. 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) a…
Xoring won’t work: - 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 numb…
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.