I thought I understand how random works

From package ret I use the following code

func init() {
    rand.Seed(time.Now().UnixNano())
}

func RandomInt(n int) int {
    return rand.Intn(n)
}

And from main.go I’m calling it like ret.RandomInt(number). But for some reason it seems uncontrolled because it gives me too many repeating results from a loop

for i := 1; i <=10; i++ { 
    y := ret.RandomInt(number)
    fmt.Println("-------", y)
}

a result could be for example like this

------- 4
------- 16
------- 16
------- 16
------- 16
------- 16
------- 16
------- 16
------- 16
------- 3

or

------- 22
------- 14
------- 14
------- 14
------- 14
------- 14
------- 14
------- 14
------- 14
------- 5

Why there are so many repeating numbers? I would prefer more random behaviour.

1 Like

It definitely shouldn’t work this way. Can you provide full reproducible example?

1 Like

Hi, GreyShatter. I can’t reproduce that behaviour again, it seems it works as expected.

1 Like

The reason of that behaviour was wrong import. Ide used import to package from previous project.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.