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

math/rand: randFloat64() gives the same result irrespective of the number of times run #13718

Closed
odeke-em opened this issue Dec 23, 2015 · 3 comments

Comments

@odeke-em
Copy link
Member

@odeke-em odeke-em commented Dec 23, 2015

In the docs at https://golang.org/pkg/math/rand/#Float64, it says
screen shot 2015-12-23 at 5 24 18 am

However, I ran tests on both the playground http://play.golang.org/p/oK5GR_9Z_N and my computer
and it always gave me the same result 0.6046602879796196 no matter how many times I ran this. I found this out when testing out a program that simulates different sleep times to simulate contention for resources.

My test was:

$ cat randTest.go
package main

import (
    "log"
    "math/rand"
)

func main() {
    log.Println(rand.Float64())
}
$ go version >> randResult.txt && for i in $(seq 1 100); do sleep 0.25;go run randTest.go 2>> randResult.txt;done;cat randResult.txt | cut -d" " -f3 | sort | uniq
0.6046602879796196
devel

And from the playground
screen shot 2015-12-23 at 5 32 13 am

That is the exact same result I got from nacl and darwin.

@odeke-em odeke-em changed the title math/rand: pseudo-rand generator randFloat64() gives the same result of 0.6046602879796196 irrespective of the number of times run math/rand: randFloat64() gives the same result irrespective of the number of times run Dec 23, 2015
@odeke-em
Copy link
Member Author

@odeke-em odeke-em commented Dec 23, 2015

Never mind: false alarm, just luck of sleep got me not to realize that since we use the same source all the time, the results are always deterministic.

@odeke-em odeke-em closed this Dec 23, 2015
@odeke-em
Copy link
Member Author

@odeke-em odeke-em commented Dec 23, 2015

I was just too used to rand* producing at least pseudo random starting values that weren't the same everytime
screen shot 2015-12-23 at 5 56 10 am

@cespare
Copy link
Contributor

@cespare cespare commented Dec 23, 2015

From the package docs: "Top-level functions, such as Float64 and Int, use a
default shared Source that produces a deterministic sequence of values each
time a program is run. Use the Seed function to initialize the default
Source if different behavior is required for each run."
On Dec 23, 2015 6:33 AM, "Emmanuel T Odeke" notifications@github.com
wrote:

In the docs at https://golang.org/pkg/math/rand/#Float64, it says
[image: screen shot 2015-12-23 at 5 24 18 am]
https://cloud.githubusercontent.com/assets/4898263/11976449/7b1ece54-a935-11e5-9f8f-cf4a28fcdd98.png

However, I ran tests on both the playground
http://play.golang.org/p/oK5GR_9Z_N and my computer
and it always gave me the same result 0.6046602879796196 no matter how
many times I ran this. I found this out when testing out a program that
simulates different sleep times to simulate contention for resources.

My test was:

$ cat randTest.go
package main

import (
"log"
"math/rand"
)

func main() {
log.Println(rand.Float64())
}

$ go version >> randResult.txt && for i in $(seq 1 100); do sleep 0.25;go run randTest.go 2>> randResult.txt;done;cat randResult.txt | cut -d" " -f3 | sort | uniq

0.6046602879796196
devel

And from the playground
screen shot 2015-12-23 at 5 32 13 am

That is the exact same result I got from nacl and darwin.


Reply to this email directly or view it on GitHub
#13718.

@golang golang locked and limited conversation to collaborators Dec 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.