publish random-numbers
This commit is contained in:
parent
79003c6c3e
commit
be7f09c8e8
@ -52,7 +52,7 @@ Regular Expressions
|
|||||||
Time
|
Time
|
||||||
Epoch
|
Epoch
|
||||||
# Time Parsing / Formatting
|
# Time Parsing / Formatting
|
||||||
# Random Numbers
|
Random Numbers
|
||||||
Number Parsing
|
Number Parsing
|
||||||
URL Parsing
|
URL Parsing
|
||||||
SHA1 Hashes
|
SHA1 Hashes
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
|
// Go's `math/rand` package provides
|
||||||
|
// [pseudorandom number](http://en.wikipedia.org/wiki/Pseudorandom_number_generator)
|
||||||
|
// generation.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// The `math/rand` package provides psuedo-random
|
|
||||||
// numbers.
|
|
||||||
import "math/rand"
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
import "math/rand"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// For example, `rand.Intn` returns a random `int` n,
|
// For example, `rand.Intn` returns a random `int` n,
|
||||||
// `0 <= n < 100`.
|
// `0 <= n < 100`.
|
||||||
fmt.Print(rand.Intn(100), ",")
|
fmt.Print(rand.Intn(100), ",")
|
||||||
@ -16,7 +19,13 @@ func main() {
|
|||||||
// `0.0 <= f < 1.0`.
|
// `0.0 <= f < 1.0`.
|
||||||
fmt.Println(rand.Float64())
|
fmt.Println(rand.Float64())
|
||||||
|
|
||||||
// To make the psuedo-random generator deterministic,
|
// This can be used to generate random floats in
|
||||||
|
// other ranges, for example `5.0 <= f' < 10.0`.
|
||||||
|
fmt.Print((rand.Float64()*5)+5, ",")
|
||||||
|
fmt.Print((rand.Float64() * 5) + 5)
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
// To make the pseudorandom generator deterministic,
|
||||||
// give it a well-known seed.
|
// give it a well-known seed.
|
||||||
s1 := rand.NewSource(42)
|
s1 := rand.NewSource(42)
|
||||||
r1 := rand.New(s1)
|
r1 := rand.New(s1)
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
$ go run random-numbers.go
|
$ go run random-numbers.go
|
||||||
81,87
|
81,87
|
||||||
0.6645600532184904
|
0.6645600532184904
|
||||||
|
7.123187485356329,8.434115364335547
|
||||||
5,87
|
5,87
|
||||||
5,87
|
5,87
|
||||||
|
|
||||||
|
|
||||||
|
# See the [`math/rand`](http://golang.org/pkg/math/rand/)
|
||||||
|
# package docs for references on other random quantities
|
||||||
|
# that Go can provide.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user