Go Package rand: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=Internal= * Standard Library * Random Number Generation in Go =Overview=") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[ | * [[Go_Language_Modularization#rand|Standard Library]] | ||
* [[Random Number Generation in Go]] | * [[Random Number Generation in Go]] | ||
=Overview= | =Overview= | ||
The package <code>math/rand</code>. | |||
=<tt>rand.New()</tt>= | |||
Generating a new random integer: | |||
<syntaxhighlight lang='go'> | |||
import ( | |||
"math/rand" | |||
"time" | |||
) | |||
i := rand.New(rand.NewSource(time.Now().UnixNano())).Int() | |||
</syntaxhighlight> | |||
Result: 8003690372325233432 | |||
=<tt>rand.NewSource()</tt>= | |||
=<tt>rand.Intn()</tt>= | |||
Generate a random number between 0 and the given argument. The following example will randomly generate 0, 1, 2, 3 and 4. | |||
<syntaxhighlight lang='go'> | |||
i := rand.Intn(5) | |||
</syntaxhighlight> |
Latest revision as of 22:31, 2 October 2023
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)