Golang Random number generator
1 min readOct 5, 2020
Random number generator is my favorite part of all kinds of languages. Here is a piece of code which can support generating random number uniformly from 0 to 1. It is using time as seed, with the changing of time, the output will be different.
package mainimport (
"fmt"
"math/rand"
"time"
)func main() {
randNumGen := rand.New(rand.NewSource(time.Now().UnixNano()))
i := 0
for {
i += 1
time.Sleep(2 * time.Second)
//fmt.Println(time.Now().UnixNano())
randNumGen = rand.New(rand.NewSource(time.Now().UnixNano()))
fmt.Println(randNumGen.Float64())
if i > 10 {
break
}
}
}