Programming Language: Golang

Namespace/Package Name: rand

Class/Type: Rand

Examples at hotexamples.com: 30

The "math/rand" package in Go provides a random number generator that returns integers and floats. The "rand" function in the "math/rand" package generates a pseudo-random float64 value in the range [0.0, 1.0). The function can generate a different sequence of random numbers for each run of a program by calling the "Seed" function first. The following are some examples of using "math/rand" package's Rand function:

**Example 1:** Generating random integers

**Example 2:** Generating random floats

import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) for i := 0; i < 5; i++ { fmt.Println(rand.Intn(10)) // generates a random integer between 0 and 10 (excluding 10) } }

import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) for i := 0; i < 5; i++ { fmt.Println(rand.Float64()) // generates a random float between 0 and 1 } }The "math/rand" package is a part of the Go standard library.

Frequently Used Methods

Related

Related in langs