Go: tour: first lesson's random number always returns 1

Created on 5 Apr 2016  路  5Comments  路  Source: golang/go

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}

this always prints:

My favorite number is 1

Is there a fixed seed? This might confuse new users, expecting the number to change every time pressing the run button.

FrozenDueToAge

Most helpful comment

I don't understand why the first lesson on Golang's website involves importing a random number generator but not providing an example for how a random generator is actually implemented? It seems confusing to not include an example of Seed.

All 5 comments

I replied via email, but it's not shown. Here is the text:

math/rand is deterministic (when used without seeding) and the
left side also gives a note:
Note: the environment in which these programs are executed is
deterministic, so each time you run the example program rand.Intn
will return the same number. (To see a different number, seed
the number generator; see rand.Seed.)

math/rand is deterministic (when used without seeding) and the
left side also gives a note:
Note: the environment in which these programs are executed is
deterministic, so each time you run the example program rand.Intn
will return the same number. (To see a different number, seed
the number generator; see rand.Seed.)

Better to add comment in the lesson's code. This will enable users to understand instantly rather than getting surprised.

For what it's worth: I was working through the tour, got confused by this as well, and ended up here.

I don't understand why the first lesson on Golang's website involves importing a random number generator but not providing an example for how a random generator is actually implemented? It seems confusing to not include an example of Seed.

Was this page helpful?
0 / 5 - 0 ratings