Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random float64 numbers in specific range using Golang

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func genRandNums(min, max float64) []float64 {
    var randNums []float64
    s := rand.NewSource(time.Now().Unix())
    r := rand.New(s)
    for x := 0; x < 10; x++ {
        // generate random float in range of min and max inclusive, append
        // to randNums and return randNums
    }
    return randNums
}

func main() {
    nums := genRandNums(1.10, 101.98)
    fmt.Println(nums)
}

I have tried searching online on how to accomplish this, but I only found out how to generate random integers in a range. Is there any way I can generate random floats in a range using Go stdlib?

like image 658
Dana Avatar asked Apr 10 '18 06:04

Dana


People also ask

How do you generate a random number in range in Golang?

Golang has built-in support for random number generation in the standard library. Specifically there is the math/rand package which implements pseudo-random number generators. Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source.

How do you generate random float numbers in Golang?

You are allowed to generate a pseudo-random number of float32 in [0.0, 1.0) from the default source with the help of the Float32() function provided by the math/rand package. So, you need to add a math/rand package in your program with the help of the import keyword.

What is Rand Intn Golang?

The rand. Intn function returns, as an int, a non-negative pseudo-random number in [0,n) from the default source. five_random.go. package main import ( "fmt" "math/rand" "time" ) func init() { rand.Seed(time.Now().UnixNano()) } func main() { for i := 0; i < 5; i++ { fmt.Printf("%d ", rand.Intn(20)) } fmt.Println() }


1 Answers

Simply use rand.Float64() to get a random number in the range of [0..1), and you can map (project) that to the range of [min..max) like this:

r := min + rand.Float64() * (max - min)

And don't create a new rand.Rand and / or rand.Source in your function, just create a global one or use the global one of the math/rand package. But don't forget to initialize it once.

Here's an example function doing that:

func randFloats(min, max float64, n int) []float64 {
    res := make([]float64, n)
    for i := range res {
        res[i] = min + rand.Float64() * (max - min)
    }
    return res
}

Using it:

func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(randFloats(1.10, 101.98, 5))
}

Output (try it on the Go Playground):

[51.43243344285539 51.92791316776663 45.04754409242326 28.77642913403846
    58.21730813384373]

Some notes:

  • The code on the Go playground will always give the same random numbers (time is fixed, so most likely the Seed will always be the same, also output is cached)
  • The above solution is safe for concurrent use, because it uses rand.Float64() which uses the global rand which is safe. Should you create your own rand.Rand using a source obtained by rand.NewSource(), that would not be safe and neither the randFloats() using it.
like image 125
icza Avatar answered Sep 18 '22 08:09

icza