jeudi 15 décembre 2022

Golang Strange random problems

A few years ago I wrote a simple "module" (https://github.com/parasit/rpgforge) to simulate dice rolls and "translate" human readable dice throw definiton to throws loops. Like "3d6" as 3 times roll six sides dice, etc.

I used it in my tools to support my hobby (pen & paper RPG). The code is simple, but so useful that I haven't really touched it for the last 4 years. However recently I needed another tool, imported my library and found the results very strange. Randomness seems to occur, but the results are returned as series of the same digits. I have no idea what's going on, and the only thing that has changed since the last time is golang from 1.18 to 1.19.

P.S. 1.19 is on win11, 1.18 is on second machine with ubuntu

Sample code:

package main

import (
    "fmt"
    "math/rand"
    "time"

    forge "github.com/parasit/rpgforge"
)

func main() {
    rand.Seed(time.Now().UTC().UnixNano())
    for x := 0; x < 10; x++ {
        fmt.Println(forge.ThrowDices("10d10"))
    }
}

Expected result (on old 1.18):

❯ go run ./main.go
{[6 6 2 6 7 7 10 3 4 3] 0}
{[1 7 7 8 6 1 3 3 6 3] 0}
{[7 4 5 1 3 9 6 7 1 1] 0}
{[1 1 7 2 10 4 5 6 10 7] 0}
{[2 10 10 1 10 3 9 10 3 6] 0}
{[2 8 1 3 1 3 5 4 6 2] 0}
{[3 9 4 5 9 2 4 7 6 6] 0}
{[3 5 7 1 6 4 8 10 9 2] 0}
{[3 6 4 8 1 7 8 10 4 1] 0}
{[2 9 2 8 9 8 5 6 7 2] 0}

But on 1.19 i got something like this:

{[1 1 1 1 1 1 1 1 1 1] 0}
{[1 1 1 1 1 1 1 1 1 1] 0}
{[1 1 1 1 1 1 1 1 1 1] 0}
{[1 1 1 1 1 1 1 1 1 1] 0}
{[8 8 8 8 8 8 8 8 8 8] 0}
{[8 8 8 8 8 8 8 8 8 8] 0}
{[8 8 8 8 8 8 8 8 8 8] 0}
{[8 8 8 8 8 8 8 8 8 8] 0}
{[8 8 8 8 8 8 8 8 8 3] 0}
{[3 3 3 3 3 3 3 3 3 3] 0}

What happened in 1.19 ???




Aucun commentaire:

Enregistrer un commentaire