Generating random numbers using SHA multiple times

194 Views Asked by At

There were similar question on stackoverflow but not exactly mine.

So i have a sequence of numbers (from 1 to ~5,000,000) (call this number N). I want to map every number to a number from [0, 99]. One solution is to take the reminder of N divided by 100. All good. I again want to do this a second time and see to which number from [0, 99] this N gets mapped to. The only requirement is that the information that the N got mapped to a number from [0, 99] (say 34) the first time should not decide anything about the number it gets mapped to the second time.

So I want to do this (SHA(N + 1) % 100) on the first time and (SHA(N + 2) % 100) the second time and so on ..

Is it guaranteed to work ? or am i missing out something?

Simply put: Take any two arbitrary numbers x1, x2 from [0, 99]. Count all numbers from [0, 5000000] which gets mapped to x1 the first time and x2 the second time. Is this count going to be the same (more or less) for any choice of x1,x2?

1

There are 1 best solutions below

1
On

I cranked out the results for 5 million values of n, SHA1(N+1)%100, and SHA1(N+2)%100, then ran it through a stats package. The sets of SHA results were uniformly distributed between 0 and 99, and the correlation matrix was:

           n      sha+1    sha+2
    n   1.0000  -0.0016  -0.0016
sha+1  -0.0016   1.0000  -0.0001
sha+2  -0.0016  -0.0001   1.0000

In other words, the SHA outcomes are effectively uncorrelated with each other. A given number 0-99 from the first SHA calculation is equally likely to be paired with any of 0-99 as the second SHA result across the set.