Generating identical random numbers in sequence after time seed? (Running on my machine)

后端 未结 2 392
既然无缘
既然无缘 2021-01-23 01:07

I\'m trying to understand precisely why, when called from an external function, my time seeded random number generator returns sequences of identical numbers.

Minimal wo

相关标签:
2条回答
  • 2021-01-23 01:27

    I'm not a Go expert but I think the problem is a generic programming issue. It is related to the fact you set the seed to each call. The seed is based on time function. So what is happening is that in a very short time, you have a number of calls while the time is not changed (yet) and so you get the same value because you are setting the same seed again and again.

    Try to set the seed, once only and outside the for loop of calls.

    0 讨论(0)
  • 2021-01-23 01:30

    This is because time.Time has a granularity (which is 1 nanosecond) just like your system clock (which might even be multiple milliseconds–depends on many things), and if you call time.Now() multiple times within the greater of these granularities, chances are the returned time.Time will be the same, meaning its Time.UnixNano() method will return you the same nanoseconds (the same number).

    And if you use the same number as the seed, the random number generator is ought to return the same numbers.

    You only need to seed the RNG once, on app startup, not before each use. You may use a package init() function for that, or in the variable declaration:

    var r = rand.New(rand.NewSource(time.Now().UnixNano()))
    
    //Generates random int as function of range
    func getRand(Range int) int {
        return r.Intn(Range)
    }
    
    //Print 100 random ints between 0 and 100
    func main() {
        for i := 0; i < 100; i++ {
            fmt.Print(getRand(100), ", ")
        }
    }
    

    Example output (try it on the Go Playground):

    0, 28, 27, 62, 63, 89, 24, 27, 88, 84, 82, 55, 49, 35, 2, 32, 84, 58, 78, 28, 26, 58, 30, 28, 74, 6, 39, 24, 40, 47, 49, 39, 61, 62, 67, 7, 94, 87, 37, 99, 90, 80, 93, 83, 27, 69, 25, 45, 99, 12, 44, 39, 34, 86, 18, 42, 76, 40, 44, 12, 70, 3, 70, 99, 57, 43, 90, 65, 97, 64, 68, 60, 65, 56, 3, 81, 54, 56, 43, 57, 92, 93, 54, 92, 9, 86, 16, 72, 29, 12, 97, 87, 55, 42, 87, 41, 94, 53, 23, 64,

    One thing to note here: rand.NewSource() returns a source which is not safe for concurrent use. If you need to call getRand() from multiple goroutines, you need to synchronize access to r, or use a separate rand.Rand in each goroutine.

    0 讨论(0)
提交回复
热议问题