Created
May 10, 2019 08:47
-
-
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
This file contains hidden or 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
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) | |
} | |
Author
meysampg
commented
May 10, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment