How to Get Float64 Type Random Number in Golang?


In Golang, generating random numbers is a common task that is required in many applications. However, the type of random number generated is important as it affects the precision and range of the number. In this article, we will discuss how to generate random Float64 type numbers in Golang.

What is Float64 Type?

Float64 is a data type in Golang that represents a floating-point number with 64 bits of precision. It is a double-precision floating-point format that can store values with a larger range and higher precision than a Float32 type. Float64 types are commonly used in scientific and engineering applications where precision is critical.

Generating Random Float64 Type Numbers in Golang

To generate a random Float64 type number in Golang, we can use the rand.Float64() function from the math/rand package. This function returns a random float64 value in the range [0.0, 1.0).

Example

Here's an example code snippet −

package main

import (
   "fmt"
   "math/rand"
)

func main() {
   // Set the seed value for the random number generator
   rand.Seed(42)

   // Generate a random Float64 type number
   randNum := rand.Float64()

   // Print the random number
   fmt.Println(randNum)
}

In this example, we first set the seed value for the random number generator using the rand.Seed() function. This ensures that the sequence of random numbers generated is deterministic and reproducible.

Next, we use the rand.Float64() function to generate a random Float64 type number and assign it to the randNum variable. Finally, we print the random number using the fmt.Println() function.

Output

0.3730283610466326

Changing the Range of Generated Float64 Type Numbers

If we want to generate a random Float64 type number within a specific range, we can use the following formula −

Syntax

rand.Float64() * (max - min) + min

where min and max are the minimum and maximum values of the range, respectively.

Example

Here's an example code snippet −

package main

import (
   "fmt"
   "math/rand"
)

func main() {
   // Set the seed value for the random number generator
   rand.Seed(42)

   // Generate a random Float64 type number between 1.0 and 10.0
   randNum := rand.Float64() * 9.0 + 1.0

   // Print the random number
   fmt.Println(randNum)
}

In this example, we generate a random Float64 type number between 1.0 and 10.0 by multiplying the result of rand.Float64() by 9.0 (the range of values we want) and then adding 1.0 (the minimum value of the range). The result is assigned to the randNum variable and printed using the fmt.Println() function.

Output

4.357255249419694

Conclusion

Generating random Float64 type numbers in Golang is easy and straightforward using the rand.Float64() function from the math/rand package. By setting the seed value for the random number generator and using the appropriate formula, we can generate random numbers within a specific range. With this knowledge, you can now incorporate random numbers into your Golang applications.

Updated on: 05-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements