publish random-numbers

This commit is contained in:
Mark McGranaghan 2012-10-31 09:31:02 -07:00
parent 79003c6c3e
commit be7f09c8e8
3 changed files with 21 additions and 6 deletions

View File

@ -52,7 +52,7 @@ Regular Expressions
Time
Epoch
# Time Parsing / Formatting
# Random Numbers
Random Numbers
Number Parsing
URL Parsing
SHA1 Hashes

View File

@ -1,11 +1,14 @@
// Go's `math/rand` package provides
// [pseudorandom number](http://en.wikipedia.org/wiki/Pseudorandom_number_generator)
// generation.
package main
// The `math/rand` package provides psuedo-random
// numbers.
import "math/rand"
import "fmt"
import "math/rand"
func main() {
// For example, `rand.Intn` returns a random `int` n,
// `0 <= n < 100`.
fmt.Print(rand.Intn(100), ",")
@ -16,7 +19,13 @@ func main() {
// `0.0 <= f < 1.0`.
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.
s1 := rand.NewSource(42)
r1 := rand.New(s1)

View File

@ -1,5 +1,11 @@
$ go run random-numbers.go
$ go run random-numbers.go
81,87
0.6645600532184904
7.123187485356329,8.434115364335547
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.