Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tour: rand.Seed suggestion does not work #12

Closed
brianshumate opened this issue Apr 28, 2016 · 4 comments
Closed

tour: rand.Seed suggestion does not work #12

brianshumate opened this issue Apr 28, 2016 · 4 comments
Assignees

Comments

@brianshumate
Copy link

brianshumate commented Apr 28, 2016

Context: https://tour.golang.org/basics/1

I wanted to get a different number each time from this example, so I took the suggestion to seed the number generator.

Here is my code:

package main

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

func main() {
    rand.Seed(time.Now().UTC().UnixNano())
    fmt.Println("My favorite number is", rand.Intn(9001))
}

When I execute the above code in the tour, the same number is returned every time as if seeding the number generator has no effect.

When I execute this code locally (go version go1.6.2 darwin/amd64) it returns a different number each time as expected.

@adg
Copy link
Contributor

adg commented Apr 29, 2016

From the page you linked:

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.)

@adg adg closed this as completed Apr 29, 2016
@adg adg reopened this Apr 29, 2016
@adg
Copy link
Contributor

adg commented Apr 29, 2016

We should probably mention that you need to seed it with something that is not the time. The time is always the same in the playground.

@adg adg self-assigned this Apr 29, 2016
@humblec
Copy link

humblec commented Nov 21, 2016

This is not good :( , it confuse lots of people. If the time is kept constant, it has to be notified somehow to an end user or at least when they use "time" package in playground.

@robphoenix
Copy link
Contributor

gopherbot pushed a commit to golang/website that referenced this issue May 26, 2021
Fixes golang/tour#12

Change-Id: I3e31796e68052c424ef6b5670aac516e461d1809
Reviewed-on: https://go-review.googlesource.com/44931
Reviewed-by: Andrew Gerrand <adg@golang.org>
X-Tour-Commit: d844dc7a157c5d2e92bccc9732530c37bc7ef33b
passionSeven added a commit to passionSeven/website that referenced this issue Oct 18, 2022
Fixes golang/tour#12

Change-Id: I3e31796e68052c424ef6b5670aac516e461d1809
Reviewed-on: https://go-review.googlesource.com/44931
Reviewed-by: Andrew Gerrand <adg@golang.org>
X-Tour-Commit: d844dc7a157c5d2e92bccc9732530c37bc7ef33b
KimuraDanaka119 added a commit to KimuraDanaka119/go-tour-jp that referenced this issue Jun 4, 2023
Fixes golang/tour#12

Change-Id: I3e31796e68052c424ef6b5670aac516e461d1809
Reviewed-on: https://go-review.googlesource.com/44931
Reviewed-by: Andrew Gerrand <adg@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants