lean into examples
This commit is contained in:
37
examples/random-numbers/random-numbers.go
Normal file
37
examples/random-numbers/random-numbers.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
// The `math/rand` package provides psuedo-random
|
||||
// numbers.
|
||||
import "math/rand"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// For example, `rand.Intn` returns a random `int` n,
|
||||
// `0 <= n < 100`.
|
||||
fmt.Print(rand.Intn(100), ",")
|
||||
fmt.Print(rand.Intn(100))
|
||||
fmt.Println()
|
||||
|
||||
// `rand.Float64` returns a `float64` `f`,
|
||||
// `0.0 <= f < 1.0`.
|
||||
fmt.Println(rand.Float64())
|
||||
|
||||
// To make the psuedo-random generator deterministic,
|
||||
// give it a well-known seed.
|
||||
s1 := rand.NewSource(42)
|
||||
r1 := rand.New(s1)
|
||||
|
||||
// Call the resulting `rand.Source` just like the
|
||||
// functions on the `rand` package.
|
||||
fmt.Print(r1.Intn(100), ",")
|
||||
fmt.Print(r1.Intn(100))
|
||||
fmt.Println()
|
||||
|
||||
// If you seed a source with the same number, it
|
||||
// produces the same sequence of random numbers.
|
||||
s2 := rand.NewSource(42)
|
||||
r2 := rand.New(s2)
|
||||
fmt.Print(r2.Intn(100), ",")
|
||||
fmt.Print(r2.Intn(100))
|
||||
fmt.Println()
|
||||
}
|
||||
5
examples/random-numbers/random-numbers.sh
Normal file
5
examples/random-numbers/random-numbers.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
$ go run random-numbers.go
|
||||
81,87
|
||||
0.6645600532184904
|
||||
5,87
|
||||
5,87
|
||||
Reference in New Issue
Block a user