Home
Go
rand, crypto: Random Number Generators
This page was last reviewed on Dec 1, 2022.
Dot Net Perls
Rand. In Go programs we get random integers by calling rand.Int. And with rand.Seed we initialize the pseudo-random number generator that the Go language uses.
Rand notes. We use "math/rand" in Go to access important random functions. We can continually call rand.Int to get a random sequence.
An intro program. This program generates 5 pseudo-random numbers. It uses a default seed value (which initializes the generator) of 1.
So The program generates the same numbers each time. Try running it multiple times—it always has the same result.
Detail This method returns a positive (non-negative) int. No fractional or negative values are possible.
Tip To change the initial series of values returned by rand.Int, use the Seed method.
package main import ( "fmt" "math/rand" ) func main() { // Loop five times. for i := 0; i < 5; i++ { // Get random positive integer. value := rand.Int() fmt.Println(value) } }
5577006791947779410 8674665223082153551 6129484611666145821 4037200794235010051 3916589616287113937
Seed. This method initializes the random source. When we use a seed based on the current time, each program execution will start with a different random sequence.
Here We use the current Nanoseconds from the time. We cast this to an int64 to seed the number generator.
Time
Note If you run this program many times, each execution will print a different random number.
package main import ( "fmt" "math/rand" "time" ) func main() { // Call Seed, using current nanoseconds. rand.Seed(int64(time.Now().Nanosecond())) // Random int will be different each program execution. value := rand.Int() fmt.Println(value) }
6912970929678809162
Rand.Perm. This function is useful. It generates slices of Ints with values from 0 to the max index of the slice. Each value appears only once. So it creates permutations of Ints.
Here We call rand.Perm with an argument of 5. So the numbers 0, 1, 2, 3, and 4 are randomly arranged in slices of 5 elements.
Important Perm uses the default random source. So we would need to use Seed to avoid the same output on each program run.
package main import ( "fmt" "math/rand" ) func main() { for i := 0; i < 5; i++ { // Use rand.Perm to generate a random array of numbers. numbers := rand.Perm(5) fmt.Println(numbers) } }
[0 4 2 3 1] [4 1 2 0 3] [2 3 0 4 1] [0 4 2 1 3] [1 0 4 2 3]
Crypto rand. Random numbers come in varying levels of randomness. For the best random numbers built into Go, we should use crypto rand. We must use the "math/big" package.
Detail We call the rand.Int method in "crypto/rand" 20 times. We get values that are from 0 to 99 inclusive (we never get 100).
package main import ( "crypto/rand" "math/big" "fmt" ) func main() { // Generate 20 random numbers with exclusive max of 100. // ... So max value returned is 99. // All values returned are 0 or greater as well. for i := 0; i < 20; i++ { result, _ := rand.Int(rand.Reader, big.NewInt(100)) fmt.Println(result) } }
54 77 56 21 56 81 55 16 57 88 95 58 12 74 23 35 72 29 25 68
Global versus local. With top-level rand methods, we use a global source. But we can create Rand instances that are local. This helps with many randomness requirements.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.