r/PowerShell • • Sep 12 '19

PowerShell All the Things

Post image
505 Upvotes

26 comments sorted by

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.

18

u/MrEpiX Sep 13 '19

I wrote code to get the closest restaurants on Google (so you'll need an API key) and then randomized among those currently open, finishing with mailing out a few alternatives to vote on using a poll.

Check it out if it sounds like fun!

https://www.pipehow.tech/get-lunch/ https://github.com/MrEpiX/get-lunch

6

u/ericrs22 Sep 12 '19

We should see if yelp has a API to improve the function

3

u/VplDazzamac Sep 12 '19

Next spare half hour I have, I might make it open a browser window to the selected choices menu on JustEat. At the minute it just outputs the name of the takeaway.

3

u/ericrs22 Sep 12 '19

You should also create personal profiles with the players top choices in cuisine. If they roll a nat 20 they get their favorite.

1

u/killahb13 Sep 12 '19

Literally came here to say this same thing. It hasn’t helped much lately though lol

1

u/foct Sep 13 '19

Use the domino's pizza api for it as well. XD

8

u/[deleted] Sep 12 '19

I love memes that teach me something new

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)

Run01
1500019499981
2500268499732
3499391500609
4499806500194
5500252499748

Get-Random @(1,0)

Run01
1500135499865
2499670500330
3499346500654
4499598500402
5499433500567

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

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

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

0.970, 0.593, 0.224, 0.699, 0.615, 0.788, 0.510, 0.191, 0.422, 0.257

None are statistically significant either.

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.

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

u/poshftw Sep 13 '19
0,1|Get-Random

1

u/lavahot Sep 13 '19

Great, now I need a dice rolling module.

0

u/[deleted] Sep 12 '19

[deleted]

6

u/[deleted] 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

u/purplemonkeymad Sep 12 '19

Technically use of a comma automatically declares an array.

10

u/BlitzThunderWolf Sep 13 '19

Although it's probably better practice to use @("heads", "tails")

3

u/poshftw Sep 13 '19
,'This is an array too!'

0

u/[deleted] Sep 13 '19

[deleted]

2

u/empty_other Sep 13 '19
1..999 | % { (1,0|Get-Random)} | Join-String

Random 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

u/BlitzThunderWolf Nov 29 '19

Your way was faster on my system by like 100ms lol