Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created May 10, 2019 08:47
Show Gist options
  • Save meysampg/92bfc4753a15dea637c473d6e7f0d208 to your computer and use it in GitHub Desktop.
Save meysampg/92bfc4753a15dea637c473d6e7f0d208 to your computer and use it in GitHub Desktop.
To show what is deterministic meaning in the rand.Intn function on golang context
package main
import "fmt"
import "math/rand"
import "time"
func constantSeed() int64 {
return 100
}
func secondSeed() int64 {
return int64(time.Now().Second())
}
func nanoSeed() int64 {
return int64(time.Now().Nanosecond())
}
func printRand(seedName string, f func () int64) {
fmt.Printf("Seeded with %s\n", seedName)
rand.Seed(f())
fmt.Println(rand.Intn(100))
fmt.Println("--------------------------------")
}
func main() {
printRand("Constant!", constantSeed)
printRand("Current Second!", secondSeed)
printRand("Current Nanosecond!", nanoSeed)
}
@meysampg
Copy link
Author

meysampg@freedom:~/www/test/trainc 
$ for i in {1..10}; do 
go run randn.go
done
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
11
--------------------------------
Seeded with Current Nanosecond!
41
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
11
--------------------------------
Seeded with Current Nanosecond!
57
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
11
--------------------------------
Seeded with Current Nanosecond!
32
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
53
--------------------------------
Seeded with Current Nanosecond!
65
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
53
--------------------------------
Seeded with Current Nanosecond!
22
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
53
--------------------------------
Seeded with Current Nanosecond!
92
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
53
--------------------------------
Seeded with Current Nanosecond!
27
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
57
--------------------------------
Seeded with Current Nanosecond!
69
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
57
--------------------------------
Seeded with Current Nanosecond!
30
--------------------------------
Seeded with Constant!
83
--------------------------------
Seeded with Current Second!
57
--------------------------------
Seeded with Current Nanosecond!
67
--------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment