This is actually expected - Get-Random returns a "fairly random" value, not a truly random value (you can't actually get them on most machines - we can get reasonably close though). Get-Random is a "get a random-ish value quickly" kind of a function, rather than a "get a really good random value".
You'll see the results are far closer (and it's far slower). The results are quite interesting to plot in Excel/your favourite spreadsheet/chart plotting software. Compare the distribution of numbers from Get-Random, Get-CryptoBit, and a version of Get-CryptoBit with the random number generator re-created each time, rather than created once and passed as a parameter.
The thing to google is "cryptographically secure pseudo-random number generator" if you're interested in what's going on here.
I was curious whether the data actually supported a bias, so I pooled all 10 runs and tested the null hypothesis that the first element is selected with probability 0.5.
Across all 10,000,000 trials:
First element selected: 5,001,554
Second element selected: 4,998,446
Using an exact two-sided binomial test:
from scipy.stats import binomtest
result = binomtest(5_001_554, n=10_000_000, p=0.5)
print(result.pvalue)
and the result:
p = 0.326
That's way above 0.05, meaning it's nowhere near statistically significant.
For completeness, I also tested each individual 1,000,000-sample run. Their p-values were approximately:
Based on these results, I don't think this dataset provides evidence that Get-Random prefers the first element. The small differences are exactly the kind of fluctuations you'd expect from random sampling.
7
u/AmazingRealist Sep 12 '19
It seems like there's a preference for whatever comes first in the array:
Looped 1m times x5:
Get-Random @(0,1)
Get-Random @(1,0)
Or most likely things would even out over more iterations and might differ from machine to machine ¯\(ツ)/¯.