summaryrefslogtreecommitdiff
path: root/devdocs/go/math%2Frand%2Fv2%2Findex.html
blob: 79488bd929af0caaaa03b8999213b1d65a7fc0e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<h1> Package rand  </h1>     <ul id="short-nav">
<li><code>import "math/rand/v2"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package rand implements pseudo-random number generators suitable for tasks such as simulation, but it should not be used for security-sensitive work. </p>
<p>Random numbers are generated by a <a href="#Source">Source</a>, usually wrapped in a <a href="#Rand">Rand</a>. Both types should be used by a single goroutine at a time: sharing among multiple goroutines requires some kind of synchronization. </p>
<p>Top-level functions, such as <a href="#Float64">Float64</a> and <a href="#Int">Int</a>, are safe for concurrent use by multiple goroutines. </p>
<p>This package's outputs might be easily predictable regardless of how it's seeded. For random numbers suitable for security-sensitive work, see the crypto/rand package. </p>   <h4 id="example_"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
answers := []string{
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes definitely",
    "You may rely on it",
    "As I see it yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it",
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
}
fmt.Println("Magic 8-Ball says:", answers[rand.IntN(len(answers))])
</pre>      <h4 id="example__rand"> <span class="text">Example (Rand)</span>
</h4> <p>This example shows the use of each of the methods on a *Rand. The use of the global functions is the same, without the receiver. </p> <p>Code:</p> <pre class="code" data-language="go">// Create and seed the generator.
// Typically a non-fixed seed should be used, such as Uint64(), Uint64().
// Using a fixed seed will produce the same output on every run.
r := rand.New(rand.NewPCG(1, 2))

// The tabwriter here helps us generate aligned output.
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
defer w.Flush()
show := func(name string, v1, v2, v3 any) {
    fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
}

// Float32 and Float64 values are in [0, 1).
show("Float32", r.Float32(), r.Float32(), r.Float32())
show("Float64", r.Float64(), r.Float64(), r.Float64())

// ExpFloat64 values have an average of 1 but decay exponentially.
show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())

// NormFloat64 values have an average of 0 and a standard deviation of 1.
show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())

// Int32, Int64, and Uint32 generate values of the given width.
// The Int method (not shown) is like either Int32 or Int64
// depending on the size of 'int'.
show("Int32", r.Int32(), r.Int32(), r.Int32())
show("Int64", r.Int64(), r.Int64(), r.Int64())
show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())

// IntN, Int32N, and Int64N limit their output to be &lt; n.
// They do so more carefully than using r.Int()%n.
show("IntN(10)", r.IntN(10), r.IntN(10), r.IntN(10))
show("Int32N(10)", r.Int32N(10), r.Int32N(10), r.Int32N(10))
show("Int64N(10)", r.Int64N(10), r.Int64N(10), r.Int64N(10))

// Perm generates a random permutation of the numbers [0, n).
show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
</pre> <p>Output:</p> <pre class="output" data-language="go">Float32     0.95955694          0.8076733            0.8135684
Float64     0.4297927436037299  0.797802349388613    0.3883664855410056
ExpFloat64  0.43463410545541104 0.5513632046504593   0.7426404617374481
NormFloat64 -0.9303318111676635 -0.04750789419852852 0.22248301107582735
Int32       2020777787          260808523            851126509
Int64       5231057920893523323 4257872588489500903  158397175702351138
Uint32      314478343           1418758728           208955345
IntN(10)    6                   2                    0
Int32N(10)  3                   7                    7
Int64N(10)  8                   9                    4
Perm        [0 3 1 4 2]         [4 1 2 0 3]          [4 3 2 0 1]
</pre>        <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#ExpFloat64">func ExpFloat64() float64</a></li>
<li><a href="#Float32">func Float32() float32</a></li>
<li><a href="#Float64">func Float64() float64</a></li>
<li><a href="#Int">func Int() int</a></li>
<li><a href="#Int32">func Int32() int32</a></li>
<li><a href="#Int32N">func Int32N(n int32) int32</a></li>
<li><a href="#Int64">func Int64() int64</a></li>
<li><a href="#Int64N">func Int64N(n int64) int64</a></li>
<li><a href="#IntN">func IntN(n int) int</a></li>
<li><a href="#N">func N[Int intType](n Int) Int</a></li>
<li><a href="#NormFloat64">func NormFloat64() float64</a></li>
<li><a href="#Perm">func Perm(n int) []int</a></li>
<li><a href="#Shuffle">func Shuffle(n int, swap func(i, j int))</a></li>
<li><a href="#Uint32">func Uint32() uint32</a></li>
<li><a href="#Uint32N">func Uint32N(n uint32) uint32</a></li>
<li><a href="#Uint64">func Uint64() uint64</a></li>
<li><a href="#Uint64N">func Uint64N(n uint64) uint64</a></li>
<li><a href="#UintN">func UintN(n uint) uint</a></li>
<li><a href="#ChaCha8">type ChaCha8</a></li>
<li> <a href="#NewChaCha8">func NewChaCha8(seed [32]byte) *ChaCha8</a>
</li>
<li> <a href="#ChaCha8.MarshalBinary">func (c *ChaCha8) MarshalBinary() ([]byte, error)</a>
</li>
<li> <a href="#ChaCha8.Seed">func (c *ChaCha8) Seed(seed [32]byte)</a>
</li>
<li> <a href="#ChaCha8.Uint64">func (c *ChaCha8) Uint64() uint64</a>
</li>
<li> <a href="#ChaCha8.UnmarshalBinary">func (c *ChaCha8) UnmarshalBinary(data []byte) error</a>
</li>
<li><a href="#PCG">type PCG</a></li>
<li> <a href="#NewPCG">func NewPCG(seed1, seed2 uint64) *PCG</a>
</li>
<li> <a href="#PCG.MarshalBinary">func (p *PCG) MarshalBinary() ([]byte, error)</a>
</li>
<li> <a href="#PCG.Seed">func (p *PCG) Seed(seed1, seed2 uint64)</a>
</li>
<li> <a href="#PCG.Uint64">func (p *PCG) Uint64() uint64</a>
</li>
<li> <a href="#PCG.UnmarshalBinary">func (p *PCG) UnmarshalBinary(data []byte) error</a>
</li>
<li><a href="#Rand">type Rand</a></li>
<li> <a href="#New">func New(src Source) *Rand</a>
</li>
<li> <a href="#Rand.ExpFloat64">func (r *Rand) ExpFloat64() float64</a>
</li>
<li> <a href="#Rand.Float32">func (r *Rand) Float32() float32</a>
</li>
<li> <a href="#Rand.Float64">func (r *Rand) Float64() float64</a>
</li>
<li> <a href="#Rand.Int">func (r *Rand) Int() int</a>
</li>
<li> <a href="#Rand.Int32">func (r *Rand) Int32() int32</a>
</li>
<li> <a href="#Rand.Int32N">func (r *Rand) Int32N(n int32) int32</a>
</li>
<li> <a href="#Rand.Int64">func (r *Rand) Int64() int64</a>
</li>
<li> <a href="#Rand.Int64N">func (r *Rand) Int64N(n int64) int64</a>
</li>
<li> <a href="#Rand.IntN">func (r *Rand) IntN(n int) int</a>
</li>
<li> <a href="#Rand.NormFloat64">func (r *Rand) NormFloat64() float64</a>
</li>
<li> <a href="#Rand.Perm">func (r *Rand) Perm(n int) []int</a>
</li>
<li> <a href="#Rand.Shuffle">func (r *Rand) Shuffle(n int, swap func(i, j int))</a>
</li>
<li> <a href="#Rand.Uint32">func (r *Rand) Uint32() uint32</a>
</li>
<li> <a href="#Rand.Uint32N">func (r *Rand) Uint32N(n uint32) uint32</a>
</li>
<li> <a href="#Rand.Uint64">func (r *Rand) Uint64() uint64</a>
</li>
<li> <a href="#Rand.Uint64N">func (r *Rand) Uint64N(n uint64) uint64</a>
</li>
<li> <a href="#Rand.UintN">func (r *Rand) UintN(n uint) uint</a>
</li>
<li><a href="#Source">type Source</a></li>
<li><a href="#Zipf">type Zipf</a></li>
<li> <a href="#NewZipf">func NewZipf(r *Rand, s float64, v float64, imax uint64) *Zipf</a>
</li>
<li> <a href="#Zipf.Uint64">func (z *Zipf) Uint64() uint64</a>
</li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_">Package</a></dd> <dd><a class="exampleLink" href="#example_IntN">IntN</a></dd> <dd><a class="exampleLink" href="#example_N">N</a></dd> <dd><a class="exampleLink" href="#example_Perm">Perm</a></dd> <dd><a class="exampleLink" href="#example_Shuffle">Shuffle</a></dd> <dd><a class="exampleLink" href="#example_Shuffle_slicesInUnison">Shuffle (SlicesInUnison)</a></dd> <dd><a class="exampleLink" href="#example__rand">Package (Rand)</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>chacha8.go</span> <span>exp.go</span> <span>normal.go</span> <span>pcg.go</span> <span>rand.go</span> <span>zipf.go</span>  </p>   <h2 id="ExpFloat64">func <span>ExpFloat64</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func ExpFloat64() float64</pre> <p>ExpFloat64 returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution whose rate parameter (lambda) is 1 and whose mean is 1/lambda (1) from the default Source. To produce a distribution with a different rate parameter, callers can adjust the output using: </p>
<pre data-language="go">sample = ExpFloat64() / desiredRateParameter
</pre> <h2 id="Float32">func <span>Float32</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Float32() float32</pre> <p>Float32 returns, as a float32, a pseudo-random number in the half-open interval [0.0,1.0) from the default Source. </p>
<h2 id="Float64">func <span>Float64</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Float64() float64</pre> <p>Float64 returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0) from the default Source. </p>
<h2 id="Int">func <span>Int</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Int() int</pre> <p>Int returns a non-negative pseudo-random int from the default Source. </p>
<h2 id="Int32">func <span>Int32</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Int32() int32</pre> <p>Int32 returns a non-negative pseudo-random 31-bit integer as an int32 from the default Source. </p>
<h2 id="Int32N">func <span>Int32N</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Int32N(n int32) int32</pre> <p>Int32N returns, as an int32, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>
<h2 id="Int64">func <span>Int64</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Int64() int64</pre> <p>Int64 returns a non-negative pseudo-random 63-bit integer as an int64 from the default Source. </p>
<h2 id="Int64N">func <span>Int64N</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Int64N(n int64) int64</pre> <p>Int64N returns, as an int64, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>
<h2 id="IntN">func <span>IntN</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func IntN(n int) int</pre> <p>IntN returns, as an int, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>   <h4 id="example_IntN"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
fmt.Println(rand.IntN(100))
fmt.Println(rand.IntN(100))
fmt.Println(rand.IntN(100))
</pre>   <h2 id="N">func <span>N</span>  </h2> <pre data-language="go">func N[Int intType](n Int) Int</pre> <p>N returns a pseudo-random number in the half-open interval [0,n) from the default Source. The type parameter Int can be any integer type. It panics if n &lt;= 0. </p>   <h4 id="example_N"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
// Print an int64 in the half-open interval [0, 100).
fmt.Println(rand.N(int64(100)))

// Sleep for a random duration between 0 and 100 milliseconds.
time.Sleep(rand.N(100 * time.Millisecond))
</pre>   <h2 id="NormFloat64">func <span>NormFloat64</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func NormFloat64() float64</pre> <p>NormFloat64 returns a normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution (mean = 0, stddev = 1) from the default Source. To produce a different normal distribution, callers can adjust the output using: </p>
<pre data-language="go">sample = NormFloat64() * desiredStdDev + desiredMean
</pre> <h2 id="Perm">func <span>Perm</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Perm(n int) []int</pre> <p>Perm returns, as a slice of n ints, a pseudo-random permutation of the integers in the half-open interval [0,n) from the default Source. </p>   <h4 id="example_Perm"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">for _, value := range rand.Perm(3) {
    fmt.Println(value)
}

</pre> <p>Output:</p> <pre class="output" data-language="go">1
2
0
</pre>   <h2 id="Shuffle">func <span>Shuffle</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Shuffle(n int, swap func(i, j int))</pre> <p>Shuffle pseudo-randomizes the order of elements using the default Source. n is the number of elements. Shuffle panics if n &lt; 0. swap swaps the elements with indexes i and j. </p>   <h4 id="example_Shuffle"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
words := strings.Fields("ink runs from the corners of my mouth")
rand.Shuffle(len(words), func(i, j int) {
    words[i], words[j] = words[j], words[i]
})
fmt.Println(words)
</pre>      <h4 id="example_Shuffle_slicesInUnison"> <span class="text">Example (SlicesInUnison)</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">
numbers := []byte("12345")
letters := []byte("ABCDE")
// Shuffle numbers, swapping corresponding entries in letters at the same time.
rand.Shuffle(len(numbers), func(i, j int) {
    numbers[i], numbers[j] = numbers[j], numbers[i]
    letters[i], letters[j] = letters[j], letters[i]
})
for i := range numbers {
    fmt.Printf("%c: %c\n", letters[i], numbers[i])
}
</pre>   <h2 id="Uint32">func <span>Uint32</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Uint32() uint32</pre> <p>Uint32 returns a pseudo-random 32-bit value as a uint32 from the default Source. </p>
<h2 id="Uint32N">func <span>Uint32N</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Uint32N(n uint32) uint32</pre> <p>Uint32N returns, as a uint32, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>
<h2 id="Uint64">func <span>Uint64</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Uint64() uint64</pre> <p>Uint64 returns a pseudo-random 64-bit value as a uint64 from the default Source. </p>
<h2 id="Uint64N">func <span>Uint64N</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func Uint64N(n uint64) uint64</pre> <p>Uint64N returns, as a uint64, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>
<h2 id="UintN">func <span>UintN</span>  <span title="Added in Go 1.22">1.22</span> </h2> <pre data-language="go">func UintN(n uint) uint</pre> <p>UintN returns, as a uint, a pseudo-random number in the half-open interval [0,n) from the default Source. It panics if n &lt;= 0. </p>
<h2 id="ChaCha8">type <span>ChaCha8</span>  <span title="Added in Go 1.22">1.22</span> </h2> <p>A ChaCha8 is a ChaCha8-based cryptographically strong random number generator. </p>
<pre data-language="go">type ChaCha8 struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="NewChaCha8">func <span>NewChaCha8</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func NewChaCha8(seed [32]byte) *ChaCha8</pre> <p>NewChaCha8 returns a new ChaCha8 seeded with the given seed. </p>
<h3 id="ChaCha8.MarshalBinary">func (*ChaCha8) <span>MarshalBinary</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (c *ChaCha8) MarshalBinary() ([]byte, error)</pre> <p>MarshalBinary implements the encoding.BinaryMarshaler interface. </p>
<h3 id="ChaCha8.Seed">func (*ChaCha8) <span>Seed</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (c *ChaCha8) Seed(seed [32]byte)</pre> <p>Seed resets the ChaCha8 to behave the same way as NewChaCha8(seed). </p>
<h3 id="ChaCha8.Uint64">func (*ChaCha8) <span>Uint64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (c *ChaCha8) Uint64() uint64</pre> <p>Uint64 returns a uniformly distributed random uint64 value. </p>
<h3 id="ChaCha8.UnmarshalBinary">func (*ChaCha8) <span>UnmarshalBinary</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (c *ChaCha8) UnmarshalBinary(data []byte) error</pre> <p>UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. </p>
<h2 id="PCG">type <span>PCG</span>  <span title="Added in Go 1.22">1.22</span> </h2> <p>A PCG is a PCG generator with 128 bits of internal state. A zero PCG is equivalent to NewPCG(0, 0). </p>
<pre data-language="go">type PCG struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="NewPCG">func <span>NewPCG</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func NewPCG(seed1, seed2 uint64) *PCG</pre> <p>NewPCG returns a new PCG seeded with the given values. </p>
<h3 id="PCG.MarshalBinary">func (*PCG) <span>MarshalBinary</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (p *PCG) MarshalBinary() ([]byte, error)</pre> <p>MarshalBinary implements the encoding.BinaryMarshaler interface. </p>
<h3 id="PCG.Seed">func (*PCG) <span>Seed</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (p *PCG) Seed(seed1, seed2 uint64)</pre> <p>Seed resets the PCG to behave the same way as NewPCG(seed1, seed2). </p>
<h3 id="PCG.Uint64">func (*PCG) <span>Uint64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (p *PCG) Uint64() uint64</pre> <p>Uint64 return a uniformly-distributed random uint64 value. </p>
<h3 id="PCG.UnmarshalBinary">func (*PCG) <span>UnmarshalBinary</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (p *PCG) UnmarshalBinary(data []byte) error</pre> <p>UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. </p>
<h2 id="Rand">type <span>Rand</span>  <span title="Added in Go 1.22">1.22</span> </h2> <p>A Rand is a source of random numbers. </p>
<pre data-language="go">type Rand struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="New">func <span>New</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func New(src Source) *Rand</pre> <p>New returns a new Rand that uses random values from src to generate other random values. </p>
<h3 id="Rand.ExpFloat64">func (*Rand) <span>ExpFloat64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) ExpFloat64() float64</pre> <p>ExpFloat64 returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution whose rate parameter (lambda) is 1 and whose mean is 1/lambda (1). To produce a distribution with a different rate parameter, callers can adjust the output using: </p>
<pre data-language="go">sample = ExpFloat64() / desiredRateParameter
</pre> <h3 id="Rand.Float32">func (*Rand) <span>Float32</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Float32() float32</pre> <p>Float32 returns, as a float32, a pseudo-random number in the half-open interval [0.0,1.0). </p>
<h3 id="Rand.Float64">func (*Rand) <span>Float64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Float64() float64</pre> <p>Float64 returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0). </p>
<h3 id="Rand.Int">func (*Rand) <span>Int</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Int() int</pre> <p>Int returns a non-negative pseudo-random int. </p>
<h3 id="Rand.Int32">func (*Rand) <span>Int32</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Int32() int32</pre> <p>Int32 returns a non-negative pseudo-random 31-bit integer as an int32. </p>
<h3 id="Rand.Int32N">func (*Rand) <span>Int32N</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Int32N(n int32) int32</pre> <p>Int32N returns, as an int32, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n &lt;= 0. </p>
<h3 id="Rand.Int64">func (*Rand) <span>Int64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Int64() int64</pre> <p>Int64 returns a non-negative pseudo-random 63-bit integer as an int64. </p>
<h3 id="Rand.Int64N">func (*Rand) <span>Int64N</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Int64N(n int64) int64</pre> <p>Int64N returns, as an int64, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n &lt;= 0. </p>
<h3 id="Rand.IntN">func (*Rand) <span>IntN</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) IntN(n int) int</pre> <p>IntN returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n &lt;= 0. </p>
<h3 id="Rand.NormFloat64">func (*Rand) <span>NormFloat64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) NormFloat64() float64</pre> <p>NormFloat64 returns a normally distributed float64 in the range -math.MaxFloat64 through +math.MaxFloat64 inclusive, with standard normal distribution (mean = 0, stddev = 1). To produce a different normal distribution, callers can adjust the output using: </p>
<pre data-language="go">sample = NormFloat64() * desiredStdDev + desiredMean
</pre> <h3 id="Rand.Perm">func (*Rand) <span>Perm</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Perm(n int) []int</pre> <p>Perm returns, as a slice of n ints, a pseudo-random permutation of the integers in the half-open interval [0,n). </p>
<h3 id="Rand.Shuffle">func (*Rand) <span>Shuffle</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Shuffle(n int, swap func(i, j int))</pre> <p>Shuffle pseudo-randomizes the order of elements. n is the number of elements. Shuffle panics if n &lt; 0. swap swaps the elements with indexes i and j. </p>
<h3 id="Rand.Uint32">func (*Rand) <span>Uint32</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Uint32() uint32</pre> <p>Uint32 returns a pseudo-random 32-bit value as a uint32. </p>
<h3 id="Rand.Uint32N">func (*Rand) <span>Uint32N</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Uint32N(n uint32) uint32</pre> <p>Uint32N returns, as a uint32, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n == 0. </p>
<h3 id="Rand.Uint64">func (*Rand) <span>Uint64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Uint64() uint64</pre> <p>Uint64 returns a pseudo-random 64-bit value as a uint64. </p>
<h3 id="Rand.Uint64N">func (*Rand) <span>Uint64N</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) Uint64N(n uint64) uint64</pre> <p>Uint64N returns, as a uint64, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n == 0. </p>
<h3 id="Rand.UintN">func (*Rand) <span>UintN</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (r *Rand) UintN(n uint) uint</pre> <p>UintN returns, as a uint, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n == 0. </p>
<h2 id="Source">type <span>Source</span>  <span title="Added in Go 1.22">1.22</span> </h2> <p>A Source is a source of uniformly-distributed pseudo-random uint64 values in the range [0, 1&lt;&lt;64). </p>
<p>A Source is not safe for concurrent use by multiple goroutines. </p>
<pre data-language="go">type Source interface {
    Uint64() uint64
}</pre> <h2 id="Zipf">type <span>Zipf</span>  <span title="Added in Go 1.22">1.22</span> </h2> <p>A Zipf generates Zipf distributed variates. </p>
<pre data-language="go">type Zipf struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="NewZipf">func <span>NewZipf</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func NewZipf(r *Rand, s float64, v float64, imax uint64) *Zipf</pre> <p>NewZipf returns a Zipf variate generator. The generator generates values k ∈ [0, imax] such that P(k) is proportional to (v + k) ** (-s). Requirements: s &gt; 1 and v &gt;= 1. </p>
<h3 id="Zipf.Uint64">func (*Zipf) <span>Uint64</span>  <span title="Added in Go 1.22">1.22</span> </h3> <pre data-language="go">func (z *Zipf) Uint64() uint64</pre> <p>Uint64 returns a value drawn from the Zipf distribution described by the Zipf object. </p><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/math/rand/v2/" class="_attribution-link">http://golang.org/pkg/math/rand/v2/</a>
  </p>
</div>