32

I am trying to generate random numbers (integers) in Go, to no avail. I found the rand package in crypto/rand, which seems to be what I want, but I can't tell from the documentation how to use it. This is what I'm trying right now:

    b := []byte{}
    something, err := rand.Read(b)
    fmt.Printf("something = %v\n", something)
    fmt.Printf("err = %v\n", err)

But unfortunately this always outputs:

    something = 0
    err = <nil>

Is there a way to fix this so that it actually generates random numbers? Alternatively, is there a way to set the upper bound on the random numbers this generates?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
  • I would expect that routine to fill the array `b` with random bytes, however many you asked for. – sarnold May 30 '11 at 22:33
  • Me too - I'm just new to Go and am not familiar with the calling conventions or the like. – Chris Bunch May 31 '11 at 00:18
  • 1
    It'll fill `b` however `b` is an empty slice (and the backing array has size 0). So rand.Read() have no space to store anything, and returns 0 in your `something` variable which indicates nothing was stored. `b := make([]byte,4)` would have been more appropriate, allowing rand.Read to store 4 bytes in b – nos Jun 06 '11 at 12:01
  • It's pretty late, but for other readers [RanGo module](https://github.com/YektaDev/RanGo) might be helpful, which is inspired by [this answer](https://stackoverflow.com/a/54491783/6155559) – YektaDev Oct 28 '20 at 15:03

4 Answers4

28

Depending on your use case, another option is the math/rand package. Don't do this if you're generating numbers that need to be completely unpredictable. It can be helpful if you need to get results that are reproducible, though -- just pass in the same seed you passed in the first time.

Here's the classic "seed the generator with the current time and generate a number" program:

package main

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

func main() {
    rand.Seed(time.Now().Unix())
    fmt.Println(rand.Int())
}
Evan Shaw
  • 23,839
  • 7
  • 70
  • 61
27

crypto/rand provides only binary stream of random data, but you can read integers from it using encoding/binary:

package main

import "encoding/binary"
import "crypto/rand"

func main() {
    var n int32
    binary.Read(rand.Reader, binary.LittleEndian, &n)
    println(n)
}
Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
17

As of 1 april 2012, after the release of the stable version of the lang, you can do the following:

package main

import "fmt" import "time" import "math/rand"

func main() { rand.Seed(time.Now().UnixNano()) // takes the current time in nanoseconds as the seed fmt.Println(rand.Intn(100)) // this gives you an int up to but not including 100 }

Ibolit
  • 9,218
  • 7
  • 52
  • 96
  • This is pretty much what I expect when asking for pseudo-random numbers. `time.Now().Unix()` doesn't quite suffice like `UnixNano()` does. – AndrewPK Jan 09 '15 at 18:08
0

You can also develop your own random number generator, perhaps based upon a simple "desert island PRNG", a Linear Congruential Generator. Also, look up L'Ecuyer (1999), Mersenne Twister, or Tausworthe generator...

https://en.wikipedia.org/wiki/Pseudorandom_number_generator

(Avoid RANDU, it was popular in the 1960's, but the random numbers generated fall on 15 hyperplanes in 3-space).

package pmPRNG

import "errors"

const (
    Mersenne31 = 2147483647 // = 2^31-1
    Mersenne31Inv = 1.0 / 2147483647.0 // = 4.656612875e-10

    // a = 16807
    a = 48271
)

// Each stream gets own seed
type PRNGStream struct {
    state int
}

func PRNGStreamNew(seed int) *PRNGStream {
    prng := (&PRNGStream{})
    prng.SetSeed(seed)
    return prng
}

// enforce seed in [1, 2^31-1]
func (r*PRNGStream) SetSeed(seed int) error {
    var err error

    if seed < 1 || seed > Mersenne31 {
        err = errors.New("Seed OOB")
    }

    if seed > Mersenne31 { seed = seed % Mersenne31 }
    if seed < 1 { seed = 1 }
    r.state = seed

    return err
}

// Dig = Park-Miller DesertIslandGenerator
// integer seed in [1, 2^31-1]
func (r*PRNGStream) Dig(seed int) float32 {
    xprev := r.state // x[i-1]
    xnext := (a * xprev) % Mersenne31 // x[i] = (a*x[i-1])%m
    r.state = xnext // x[i-1] = x[i]
    Ri := float32(xnext) * Mersenne31Inv // convert Ui to Ri
    return Ri
}


func (r*PRNGStream) Rand() float32 {
    r.state = (uint64_t)*r.state * Multby % 0x7fffffff
    return float32(r.state) * Mersenne31Inv
}

A few relevant links:

https://en.wikipedia.org/wiki/Lehmer_random_number_generator

You might use this function to update your x[i+1], instead of the one above, val = ((state * 1103515245) + 12345) & 0x7fffffff (basically, different values of a, c, m)

https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux

https://www.iro.umontreal.ca/~lecuyer/myftp/papers/handstat.pdf

https://www.math.utah.edu/~alfeld/Random/Random.html

https://learn.microsoft.com/en-us/archive/msdn-magazine/2016/august/test-run-lightweight-random-number-generation

ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42