4

I'm new to Go and not sure why it prints the same number for rand.Intn(n int) int for every run:

package main

import (
    "fmt"
    "math/rand"
)


func main() {
    fmt.Println(rand.Intn(10)) 
}

The docs says :

Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.

And how do I properly seed the random number generation?

Dave C
  • 7,729
  • 4
  • 49
  • 65
mjlowky
  • 1,183
  • 12
  • 19

3 Answers3

13

By calling the rand.Seed() function, passing it a (random) seed (typically the current unix timestamp). Quoting from math/rand package doc:

Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run. Use the Seed function to initialize the default Source if different behavior is required for each run.

Example:

rand.Seed(time.Now().UnixNano())

If rand.Seed() is not called, the generator behaves as if seeded by 1:

Seed uses the provided seed value to initialize the default Source to a deterministic state. If Seed is not called, the generator behaves as if seeded by Seed(1).

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thanks @icza. So the default `seed` is some constant ? – mjlowky Aug 18 '17 at 09:54
  • 3
    why would a function exist that is named "rand" when it does not do anything random, unless a second function is being called? What would be the use-case of not wanting to have a random number each time? E.g. why are both functionalities not combined by default? – lifeofguenter Oct 13 '19 at 08:14
  • 3
    @lifeofguenter Because this gives you the possibility to repeat, to "replay" the same pseudo-random sequence of numbers (by using the same seed at the beginning of the operation). That's a useful property. – icza Oct 13 '19 at 08:15
2
package main

import

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

func randomGen(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max - min) + min
}

func main() {
randNum := randomGen(1, 10)
fmt.Println(randNum)
}
elcomendante
  • 1,113
  • 1
  • 11
  • 28
  • 2
    Do *not* re-seed on every call. – Dave C Aug 11 '19 at 12:24
  • @elcomendaante: This is the reason you should not seed it each call. Do change your code: https://stackoverflow.com/questions/20636859/random-number-generator-why-seed-every-time – Nav Sep 20 '21 at 02:35
-1

Under the package, math/rand, you can find a type Rand.

func New(src Source) *Rand - New returns a new Rand that uses random values from src to generate other random values.

It actually needs a seed to generate it.

  1. step 1: create a seed as a source using new Source. time.Now().UnixNano() is used for the accuracy.
  2. step 2: create a type Rand from the seed
  3. step 3: generate a random number.

Example:

package main

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

func main() {  
    source := rand.NewSource(time.Now().UnixNano())    
    r := rand.New(source)    
    fmt.Println(r.Intn(100))    
}
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
Azhagu Surya
  • 132
  • 1
  • 1
  • 17