Re:Predicting Random Numbers in Ethereum Smart Contracts
by loverofthings on Saturday February 03, 2018 @03:31AM
(#16296785)
Well, let's ignore the reuse of the seeds, let's focus on generating random numbers in a range 0-N using a uniform random generator that gives numbers from 0-M where M >= N.
Code snippets shown use modulus (x % (N+1)) to accomplish that. This will result in the numbers not at all uniformly distributed.
Of course, the differences in probability aren't that huge but the contracts are mathematically not fair.
import numpy as np
n, m = 2, 20
binc = np.bincount(np.random.randint(0, m, size=20000000) % (n+1))
print 1.0*binc/sum(binc);
[0.3501464 0.3497516 0.300102 ]
When we're generating 0-2, from 0-20, we get 0 and 1 more often than 2.
Code snippets shown use modulus (x % (N+1)) to accomplish that. This will result in the numbers not at all uniformly distributed.
Of course, the differences in probability aren't that huge but the contracts are mathematically not fair.
When we're generating 0-2, from 0-20, we get 0 and 1 more often than 2.