Programming Language: Golang

Namespace/Package Name: math/rand

Class/Type: Rand

Method/Function: Float64

Examples at hotexamples.com: 30

The "math/rand" package in Go language provides various functions for generating random float64 numbers. The most commonly used function is "Rand.Float64()", which generates a float64 number in the range 0.0 <= f < 1.0.

**Example 1:** Generate a random float64 number between 0.0 and 1.0

Output:

**Example 2:** Generate a random float64 number between a specified lower and upper bound

Output:

package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) fmt.Println(rand.Float64()) }

Output:

0.7292459645420822

package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) lowerBound := 1.0 upperBound := 10.0 randomNum := lowerBound + rand.Float64()*(upperBound-lowerBound) fmt.Println(randomNum) }

Output:

7.039421919958845In both examples, we used the "math/rand" package to generate a random float64 number. We also used the "time" package to seed the random number generator, which ensures that we get a different sequence of random numbers each time we run the program.

Frequently Used Methods

Frequently Used Methods

Related

Related in langs