MichaelTJones / pcg

Go implementation of Melissa O'Neill's excellent PCG pseudorandom number generator
Apache License 2.0
59 stars 5 forks source link

Seeding and the meaning of state and sequence #5

Open Pushplaybang opened 5 years ago

Pushplaybang commented 5 years ago

Hi Micheal,

Great package, I'm both new to go, and the challenges of RNG, and I was wondering if you could provide more insight into how to seed this correctly.

I created a working example, in which I managed to generate an even distribution, in a seemingly non-deterministic way, but feel a bit like a monkey bashing the keyboard. (I've included results for a small range below, but have run this with multiple ranges, up to 1m)

package main

import (
    "fmt"
    "github.com/MichaelTJones/pcg"
    "sort"
    "time"
)

func main() {
    st1 := uint64(time.Time(time.Now()).UnixNano())
    st2 := uint64(time.Time(time.Now()).UnixNano()<<32)
    rng := pcg.NewPCG64().Seed(st1, st2, 1, 1)

    //batch := map[uint64]uint64{}
    count := map[uint64]uint64{}

    // roll the dice
    for i := 100000000; i >= 1; i-- {
        num := rng.Bounded(6)+1
        count[num] = count[num] + 1
    }

    // To store the keys in slice in sorted order
    var keys []int
    for k := range count {
        keys = append(keys, int(k))
    }
    sort.Ints(keys)

    for _, k := range keys {
        fmt.Printf("counts: %v %v \n", k, count[uint64(k)])
    }
}

This seems to produce reasonably good results, but if you could enlighten me with regards to the seed arguments that would be amazing.