Live data from Hacker News

Evolution of Random Number Generators

johndcook.com

1–10 of 56 posts

Re: Evolution of Random Number Generators

#2
I 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) and xor the number with some other number? Maybe with 0101010...?

Re: Evolution of Random Number Generators

#3
post #2

I 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…

Are you talking about generating a random permutation?

Re: Evolution of Random Number Generators

#4
post #2

I 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…

Are you talking about generating a random permutation?

I don't think so. Maybe a "random sequence" could be a name for it. But not sure.

Re: Evolution of Random Number Generators

#5
post #4

Earlier quoted context omitted.

Are you talking about generating a random permutation?

I don't think so. Maybe a "random sequence" could be a name for it. But not sure.

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?

Re: Evolution of Random Number Generators

#6
post #4

Earlier quoted context omitted.

I don't think so. Maybe a "random sequence" could be a name for it. But not sure.

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

#7
post #2

I 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…

Look into linear feedback shift registers, they are very simple and can produce random-looking permutations.

Re: Evolution of Random Number Generators

#8
post #6

Earlier 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.

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 here:

https://stackoverflow.com/questions/29236556/how-can-i-calcu...

Re: Evolution of Random Number Generators

#9
post #6

Earlier 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.

for JS you can probably just do `[...Array(n).keys()].sort(()=>Math.random() - 0.5)` ?

Re: Evolution of Random Number Generators

#10
post #6

Earlier 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.

If your value of size is a power of two you could just encrypt x with a block cipher where the block size is equal to size and the key is your seed.
Post reply on HN