Last active
December 19, 2015 12:19
-
-
Save r3b/5954541 to your computer and use it in GitHub Desktop.
Go's Pseudo-random number generator isn't very pseudo. In fact, it's not random at all.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://code.google.com/p/go/issues/detail?id=4509 | |
package main | |
import ( | |
"fmt" | |
"time" | |
"math/rand" | |
) | |
var out = make(chan string, 3) | |
var done = make(chan bool) | |
func f(c chan<- string) { | |
for i := 0; i<10 ; i++ { | |
c<-(time.Millisecond * time.Duration(rand.Intn(1000))).String() | |
} | |
time.Sleep(time.Millisecond * 10) | |
done<-true | |
} | |
func printer(c <-chan string) { | |
for {fmt.Println(<- c)} | |
} | |
//initialize the random seed to the current time | |
func init(){ | |
rand.Seed(time.Now().UnixNano()) | |
} | |
func main() { | |
go f(out) | |
go printer(out) | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment