mercredi 30 juin 2021

golang rand.Int(). Why every time same values?

I have a code:

package main

import ("fmt"
    "math/rand"
    "strconv"
)

func main() {
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
    fmt.Println(strconv.Itoa(rand.Int()))
}

When I run it (go run code.go), I get every time same values:

5577006791947779410
8674665223082153551
6129484611666145821
4037200794235010051
3916589616287113937
6334824724549167320
605394647632969758
1443635317331776148
894385949183117216
2775422040480279449
4751997750760398084
7504504064263669287
1976235410884491574
3510942875414458836

Second try:

package main

import ("fmt"
        "math/rand"
        "strconv"
)

func main() {
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
    fmt.Println(strconv.Itoa(rand.Intn(100)))
}

Same behaviour. Every time's

81
87
47
59
81
18
25

Is this a joke? Why it happens? Here is no description about non-random same result. I can see only pseudo-random term without explanation what that means.

Looks like even bash is more logical and stable...

random




Aucun commentaire:

Enregistrer un commentaire