Skip to content

Instantly share code, notes, and snippets.

@r3b
Last active December 19, 2015 12:19
Show Gist options
  • Save r3b/5954541 to your computer and use it in GitHub Desktop.
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.
//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