8
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)
| Run | 0 | 1 |
|---|---|---|
| 1 | 500019 | 499981 |
| 2 | 500268 | 499732 |
| 3 | 499391 | 500609 |
| 4 | 499806 | 500194 |
| 5 | 500252 | 499748 |
Get-Random @(1,0)
| Run | 0 | 1 |
|---|---|---|
| 1 | 500135 | 499865 |
| 2 | 499670 | 500330 |
| 3 | 499346 | 500654 |
| 4 | 499598 | 500402 |
| 5 | 499433 | 500567 |
Or most likely things would even out over more iterations and might differ from machine to machine ¯\(ツ)/¯.
4
u/mrmonday Sep 13 '19
This is actually expected -
Get-Randomreturns 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-Randomis a "get a random-ish value quickly" kind of a function, rather than a "get a really good random value".Stealing/adapting /u/Szeraax's code from below:
function Get-CryptoBit { param ($rng) $byte = New-Object -TypeName byte[] -ArgumentList 1 $rng.GetBytes($byte) $byte[0] % 2 } $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() 1..10 | %{,(1..10000 | %{Get-CryptoBit $rng} | group | sort-object Name) | % {[pscustomobject]@{$_[0].name = $_[0].count;$_[1].name = $_[1].count;}}}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 ofGet-CryptoBitwith 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.
1
u/fatherjack9999 Sep 13 '19
"a really good random number" :D
1
1
u/Inothernews1 Jul 03 '26 edited Jul 22 '26
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.326That'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:
0.970, 0.593, 0.224, 0.699, 0.615, 0.788, 0.510, 0.191, 0.422, 0.257None are statistically significant either.
Based on these results, I don't think this dataset provides evidence that
Get-Randomprefers the first element. The small differences are exactly the kind of fluctuations you'd expect from random sampling.
2
u/MadnessDude Sep 13 '19
We're three guys at work, and sometimes we need a "coint-toss" to select which admin does the task, here is our solution..
Function Allocate-Worker {
$Crap = @()
Foreach ($User in 1..9999){
$Players = "Player1","Player2","Player3"
$Random = $Players | Get-random
$CustomObject = New-Object psobject
Add-Member -InputObject $CustomObject -MemberType NoteProperty -Name "WhoWillItBe" -Value $Random
$Crap += $CustomObject
}
$Player1 = @($Crap | ? {$_.WhoWillItBe -eq "Player1"}).count
$Player2 = @($Crap | ? {$_.WhoWillItBe -eq "Player2"}).count
$Player3 = @($Crap | ? {$_.WhoWillItBe -eq "Player3"}).count
$Player1Calculated = "{0:p1}" -f ($Player1/9999)
$Player2Calculated = "{0:p1}" -f ($Player2/9999)
$Player3Calculated = "{0:p1}" -f ($Player3/9999)
$PrecentageTable = @{
"Player1" = $Player1Calculated
"Player2" = $Player2Calculated
"Player3" = $Player3Calculated
}
$Precentages = $PrecentageTable.GetEnumerator() | sort -Property Value -Descending
$Precentages | ft
Write-Host "And the winner of this task is $($Precentages.Name | select -First 1)!" -ForegroundColor Magenta
}2
1
0
Sep 12 '19
[deleted]
6
Sep 12 '19
I was just wondering if you could pass a string array to get random. Looks like you don't even actually have to declare it as an array.
30
0
Sep 13 '19
[deleted]
2
u/empty_other Sep 13 '19
1..999 | % { (1,0|Get-Random)} | Join-StringRandom binary generator! :P
2
u/BlitzThunderWolf Nov 29 '19 edited Nov 29 '19
Wouldn't it be more efficient to use get-random without a pipe? Like: get-random 0,1
Edit: I tested it and was wrong
1
u/empty_other Nov 29 '19
You are right, your way is slightly faster on my system. Around 2% faster.
2
24
u/VplDazzamac Sep 12 '19
In work we used to have a lunch dice. I replaced it with a function to decide where to order lunch from on payday Friday. It was one of my more productive days.