Go Package rand

From NovaOrdis Knowledge Base
Revision as of 22:31, 2 October 2023 by Ovidiu (talk | contribs) (→‎rand.Intn())
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

The package math/rand.

rand.New()

Generating a new random integer:

import (
  "math/rand"
  "time"
)

i := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

Result: 8003690372325233432

rand.NewSource()

rand.Intn()

Generate a random number between 0 and the given argument. The following example will randomly generate 0, 1, 2, 3 and 4.

i := rand.Intn(5)