Skip to content

rand.Intn should not return a fixed number sequence #4509

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

Closed
gopherbot opened this issue Dec 9, 2012 · 2 comments
Closed

rand.Intn should not return a fixed number sequence #4509

gopherbot opened this issue Dec 9, 2012 · 2 comments
Milestone

Comments

@gopherbot
Copy link
Contributor

by nanjunjie:

From document of rand.Intn:
Intn returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n
<= 0.

I found it always returns a fixed number sequence. For example on my machine, the
sequence for Intn(10) is: :8, 6, 6, 7, 0...

What steps will reproduce the problem?
Source:
package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println(rand.Intn(10))
}

Link: http://play.golang.org/p/1EUY0jaitW

What is the expected output?
It should return a random number. 

What do you see instead?
On play.golang.org, the output is always 1 for each run. On my machine, the output is
always 8.

Which operating system are you using?
Ubuntu 12.04

Which version are you using?  (run 'go version')
go version devel +ea15e7ed6d72 Sun Dec 09 03:59:33 2012 -0500

Please provide any additional information below.
@rsc
Copy link
Contributor

rsc commented Dec 9, 2012

Comment 1:

You need to seed the generator. For example: rand.Seed(time.Now().UnixNano()).

@rsc
Copy link
Contributor

rsc commented Dec 9, 2012

Comment 2:

The new example (http://tip.golang.org/pkg/math/rand/#example_) will
be in Go 1.1 and should make this clear.

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants