samedi 2 mai 2020

Is there any way to improve this Powershell array shuffle method?

I developped a card deck to play board game on Visio during the Covid. My friends and I have the "feeling" the cards are not well shuffled. Note to downvoters: I don't know how to measure properly this feeling on a true mathemical/programmer way. But I would be more than happy to properly measure it.

The code I am using is the following (I simplified but I am using the same algorithm for the verso of the cards which contains numbers, if needed I can add it):

    # Actions
    $CONFIG_ACTIONS = @{
        "pool"=9
        "interim"=9
        "bis"=9
        "parc"=18
        "sell"=18
        "barrier"=18
    }
    $unshuffeledListAction = @()
    foreach ($actionToAdd in $CONFIG_ACTIONS.Keys) {
        [int]$howManyTime = $($CONFIG_ACTIONS[$actionToAdd])
        for($i=0;$i -lt $howManyTime;$i++) {
            $unshuffeledListAction += $actionToAdd
        }
    }
    # Request random number between 0 and 65535
    $requestURI = "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint16"
    # Randomize Cards: Try WebService
    Try {
        $random = $(Invoke-RestMethod -Uri $requestURI -Method GET).data 
        $shuffeledListActions = $unshuffeledListAction | Get-Random -Count $unshuffeledListAction.Count -SetSeed ($random[0])       
    } Catch {
        Write-warning "Failed to called QRNG@ANU JSON API switching to local pseudo-random"
        $shuffeledListActions = $unshuffeledListAction | Sort-Object {Get-Random}
    }

I have no special needs on security as this is for personnal use without sensitive information so I am OK to work with WebServices and I already made a try.

My first shuffle attempt was simply : $shuffeledListActions = $unshuffeledListAction | Sort-Object {Get-Random} can it be improved ? The second attempt with calling a quantum random number generator service is better ?




Aucun commentaire:

Enregistrer un commentaire