Skip to content

Commit 31ed482

Browse files
committed
update std/math/rand
1 parent b98d925 commit 31ed482

File tree

1 file changed

+270
-37
lines changed

1 file changed

+270
-37
lines changed

src/std/math-rand.md

Lines changed: 270 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,200 @@
22

33
## Index
44

5-
[fn NewSource\(seed: u64\): Source](#newsource)\
5+
[fn New\(src: Source\): &Rand](#new)\
6+
[fn I64\(\): i64](#i64)\
7+
[fn U32\(\): u32](#u32)\
8+
[fn U64N\(n: u64\): u64](#u64n)\
9+
[fn U32N\(n: u32\): u32](#u32n)\
10+
[fn U64\(\): u64](#u64)\
11+
[fn I32\(\): i32](#i32)\
12+
[fn Int\(\): int](#int)\
13+
[fn Uint\(\): uint](#uint)\
14+
[fn I64N\(n: i64\): i64](#i64n)\
15+
[fn Int32N\(n: i32\): i32](#int32n)\
16+
[fn IntN\(n: int\): int](#intn)\
17+
[fn UintN\(n: uint\): uint](#uintn)\
18+
[fn F64\(\): f64](#f64)\
19+
[fn F32\(\): f32](#f32)\
20+
[fn Perm\(n: int\): \[\]int](#perm)\
21+
[fn Shuffle\(n: int, swap: fn\(i: int, j: int\)\)](#shuffle)\
22+
[fn NormF64\(\): f64](#normf64)\
23+
[fn ExpFloat64\(\): f64](#expfloat64)\
24+
[fn NewZipf\(r: &Rand, s: f64, v: f64, imax: u64\): &Zipf](#newzipf)\
25+
[fn NewPCG\(seed1: u64, seed2: u64\): &PCG](#newpcg)\
626
[trait Source](#source)\
727
[struct Rand](#rand)\
8-
    [fn New\(src: Source\): &Rand](#new)\
9-
    [fn U64\(\*self\): u64](#u64)\
10-
    [fn U32\(\*self\): u32](#u32)\
11-
    [fn I64\(\*self\): i64](#i64)\
12-
    [fn I32\(\*self\): i32](#i32)\
13-
    [fn Int\(\*self\): int](#int)\
14-
    [fn U64n\(\*self, n: u64\): u64](#u64n)\
15-
    [fn I64n\(\*self, n: i64\): i64](#i64n)\
16-
    [fn I32n\(\*self, n: i32\): i32](#i32n)\
17-
    [fn U32n\(\*self, n: u32\): u32](#u32n)\
18-
    [fn Intn\(\*self, n: int\): int](#intn)\
19-
    [fn Uintn\(\*self, n: uint\): uint](#uintn)\
20-
    [fn F64\(\*self\): f64](#f64)\
21-
    [fn F32\(\*self\): f32](#f32)
28+
    [fn NormF64\(\*self\): f64](#normf64-1)\
29+
    [fn U64\(\*self\): u64](#u64-1)\
30+
    [fn U32\(\*self\): u32](#u32-1)\
31+
    [fn I64\(\*self\): i64](#i64-1)\
32+
    [fn I32\(\*self\): i32](#i32-1)\
33+
    [fn Int\(\*self\): int](#int-1)\
34+
    [fn Uint\(\*self\): uint](#uint-1)\
35+
    [fn U64N\(\*self, n: u64\): u64](#u64n-1)\
36+
    [fn I64N\(\*self, n: i64\): i64](#i64n-1)\
37+
    [fn I32N\(\*self, n: i32\): i32](#i32n)\
38+
    [fn U32N\(\*self, n: u32\): u32](#u32n-1)\
39+
    [fn IntN\(\*self, n: int\): int](#intn-1)\
40+
    [fn UintN\(\*self, n: uint\): uint](#uintn-1)\
41+
    [fn F64\(\*self\): f64](#f64-1)\
42+
    [fn F32\(\*self\): f32](#f32-1)\
43+
    [fn Perm\(\*self, n: int\): \[\]int](#perm-1)\
44+
    [fn Shuffle\(\*self, n: int, swap: fn\(i: int, j: int\)\)](#shuffle-1)\
45+
    [fn ExpF64\(\*self\): f64](#expf64)\
46+
[struct Zipf](#zipf)\
47+
    [fn U64\(\*self\): u64](#u64-2)\
48+
[struct PCG](#pcg)\
49+
    [fn Seed\(\*self, seed1: u64, seed2: u64\)](#seed)\
50+
    [fn U64\(\*self\): u64](#u64-3)
2251

2352

2453

25-
## NewSource
54+
## New
2655
```jule
27-
fn NewSource(seed: u64): Source
56+
fn New(src: Source): &Rand
57+
```
58+
Returns new Rand that uses random values from src to generate other random values\.
59+
60+
## I64
61+
```jule
62+
fn I64(): i64
63+
```
64+
Returns a non\-negative pseudo\-random 63\-bit integer as an i64 from the default Source\.
65+
66+
## U32
67+
```jule
68+
fn U32(): u32
69+
```
70+
Returns a pseudo\-random 32\-bit value as a u32 from the default Source\.
71+
72+
## U64N
73+
```jule
74+
fn U64N(n: u64): u64
75+
```
76+
Returns, as a u64, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n == 0\.
77+
78+
## U32N
79+
```jule
80+
fn U32N(n: u32): u32
81+
```
82+
Returns, as a u32, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n == 0\.
83+
84+
## U64
85+
```jule
86+
fn U64(): u64
87+
```
88+
Returns a pseudo\-random 64\-bit value as a u64 from the default Source\.
89+
90+
## I32
91+
```jule
92+
fn I32(): i32
93+
```
94+
Returns a non\-negative pseudo\-random 31\-bit integer as an i32 from the default Source\.
95+
96+
## Int
97+
```jule
98+
fn Int(): int
99+
```
100+
Returns a non\-negative pseudo\-random int from the default Source\.
101+
102+
## Uint
103+
```jule
104+
fn Uint(): uint
105+
```
106+
Returns a pseudo\-random uint from the default Source\.
107+
108+
## I64N
109+
```jule
110+
fn I64N(n: i64): i64
111+
```
112+
Returns, as an i64, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n <= 0\.
113+
114+
## Int32N
115+
```jule
116+
fn Int32N(n: i32): i32
117+
```
118+
Returns, as an i32, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n <= 0\.
119+
120+
## IntN
121+
```jule
122+
fn IntN(n: int): int
123+
```
124+
Returns, as an int, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n <= 0\.
125+
126+
## UintN
127+
```jule
128+
fn UintN(n: uint): uint
129+
```
130+
Returns, as a uint, a pseudo\-random number in the half\-open interval \[0,n\) from the default Source\. It panics if n == 0\.
131+
132+
## F64
133+
```jule
134+
fn F64(): f64
135+
```
136+
Returns, as a f64, a pseudo\-random number in the half\-open interval \[0\.0,1\.0\) from the default Source\.
137+
138+
## F32
139+
```jule
140+
fn F32(): f32
141+
```
142+
Returns, as a f32, a pseudo\-random number in the half\-open interval \[0\.0,1\.0\) from the default Source\.
143+
144+
## Perm
145+
```jule
146+
fn Perm(n: int): []int
147+
```
148+
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\.
149+
150+
## Shuffle
151+
```jule
152+
fn Shuffle(n: int, swap: fn(i: int, j: int))
153+
```
154+
Shuffle pseudo\-randomizes the order of elements using the default Source\. n is the number of elements\. It panics if n < 0\. swap swaps the elements with indexes i and j\.
155+
156+
## NormF64
157+
```jule
158+
fn NormF64(): f64
159+
```
160+
Returns a normally distributed f64 in the range \[\-f64\.Max, \+f64\.Max\] with standard normal distribution \(mean = 0, stddev = 1\) from the default Source\. To produce a different normal distribution, callers can adjust the output using:
161+
162+
```
163+
sample = NormF64() * desiredStdDev + desiredMean
164+
```
165+
166+
167+
## ExpFloat64
168+
```jule
169+
fn ExpFloat64(): f64
170+
```
171+
Returns an exponentially distributed f64 in the range \(0, \+f64\.Max\] 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:
172+
173+
```
174+
sample = ExpF64() / desiredRateParameter
28175
```
29-
Returns new default source by seed\.
30176

31-
The order and numbers produced vary depending on the seed\. Since PRNGs are inherently deterministic, using a fixed seed means your program will generate the same numbers every time\.
32177

33-
If you want to achieve randomness somehow, use a variable seed\. A simple solution for seeds that will create the illusion of randomness is to use time\. Unix\-time seconds would be a simple seed solution\.
178+
## NewZipf
179+
```jule
180+
fn NewZipf(r: &Rand, s: f64, v: f64, imax: u64): &Zipf
181+
```
182+
Returns a Zipf variate generator\. The generator generates values k ∈ \[0, imax\] such that P\(k\) is proportional to \(v \+ k\) \*\* \(\-s\)\. Requirements: s > 1 and v >= 1\.
183+
184+
## NewPCG
185+
```jule
186+
fn NewPCG(seed1: u64, seed2: u64): &PCG
187+
```
188+
Returns a new PCG seeded with the given values\.
34189

35190
## Source
36191
```jule
37192
trait Source {
38193
fn U64(*self): u64
39194
}
40195
```
41-
Source of uniformly\-distributed pseudo\-random u64 values in the range \[0, 1<<64\)\. It is not safe for concurrent use by multiple threads\.
196+
A Source is a source of uniformly\-distributed pseudo\-random u64 values in the range \[0, 1<<64\)\.
197+
198+
It is not safe for concurrent use by multiple threads\.
42199

43200
## Rand
44201
```jule
@@ -48,11 +205,16 @@ struct Rand {
48205
```
49206
Implements a type of pseudo random number generator \(PRNG\)\. Outputs might be easily predictable regardless of how it's seeded\. For random numbers suitable for security\-sensitive work, it is not recommended\.
50207

51-
### New
208+
### NormF64
52209
```jule
53-
fn New(src: Source): &Rand
210+
fn NormF64(*self): f64
211+
```
212+
Returns a normally distributed f64 in the range \[\-f64\.Max, \+f64\.Max\] with standard normal distribution \(mean = 0, stddev = 1\) from the default Source\. To produce a different normal distribution, callers can adjust the output using:
213+
214+
```
215+
sample = NormF64() * desiredStdDev + desiredMean
54216
```
55-
Returns new Rand that uses random values from src to generate other random values\.
217+
56218

57219
### U64
58220
```jule
@@ -84,39 +246,45 @@ fn Int(*self): int
84246
```
85247
Returns a non\-negative pseudo\-random int\.
86248

87-
### U64n
249+
### Uint
250+
```jule
251+
fn Uint(*self): uint
252+
```
253+
Returns a pseudo\-random uint\.
254+
255+
### U64N
88256
```jule
89-
fn U64n(*self, n: u64): u64
257+
fn U64N(*self, n: u64): u64
90258
```
91259
Returns, as a u64, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n == 0\.
92260

93-
### I64n
261+
### I64N
94262
```jule
95-
fn I64n(*self, n: i64): i64
263+
fn I64N(*self, n: i64): i64
96264
```
97265
Returns, as an i64, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n == 0\.
98266

99-
### I32n
267+
### I32N
100268
```jule
101-
fn I32n(*self, n: i32): i32
269+
fn I32N(*self, n: i32): i32
102270
```
103271
Returns, as an i32, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n <= 0\.
104272

105-
### U32n
273+
### U32N
106274
```jule
107-
fn U32n(*self, n: u32): u32
275+
fn U32N(*self, n: u32): u32
108276
```
109277
Returns, as a u32, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n == 0\.
110278

111-
### Intn
279+
### IntN
112280
```jule
113-
fn Intn(*self, n: int): int
281+
fn IntN(*self, n: int): int
114282
```
115283
Returns, as an int, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n <= 0\.
116284

117-
### Uintn
285+
### UintN
118286
```jule
119-
fn Uintn(*self, n: uint): uint
287+
fn UintN(*self, n: uint): uint
120288
```
121289
Returns, as a uint, a non\-negative pseudo\-random number in the half\-open interval \[0,n\)\. It panics if n == 0\.
122290

@@ -130,4 +298,69 @@ Returns, as a f64, a pseudo\-random number in the half\-open interval \[0\.0,1\.
130298
```jule
131299
fn F32(*self): f32
132300
```
133-
Returns, as a f32, a pseudo\-random number in the half\-open interval \[0\.0,1\.0\)\.
301+
Returns, as a f32, a pseudo\-random number in the half\-open interval \[0\.0,1\.0\)\.
302+
303+
### Perm
304+
```jule
305+
fn Perm(*self, n: int): []int
306+
```
307+
Returns, as a slice of n ints, a pseudo\-random permutation of the integers in the half\-open interval \[0,n\)\.
308+
309+
### Shuffle
310+
```jule
311+
fn Shuffle(*self, n: int, swap: fn(i: int, j: int))
312+
```
313+
Shuffle pseudo\-randomizes the order of elements\. n is the number of elements\. It panics if n < 0\. swap swaps the elements with indexes i and j\.
314+
315+
### ExpF64
316+
```jule
317+
fn ExpF64(*self): f64
318+
```
319+
Returns an exponentially distributed f64 in the range \(0, \+f64\.Max\] 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:
320+
321+
```
322+
sample = ExpF64() / desiredRateParameter
323+
```
324+
325+
326+
## Zipf
327+
```jule
328+
struct Zipf {
329+
// NOTE: contains filtered hidden or unexported fields
330+
}
331+
```
332+
A Zipf generates Zipf distributed variates\.
333+
334+
### Implemented Traits
335+
336+
- `Source`
337+
338+
### U64
339+
```jule
340+
fn U64(*self): u64
341+
```
342+
Returns a value drawn from the Zipf distribution described by the Zipf object\.
343+
344+
## PCG
345+
```jule
346+
struct PCG {
347+
// NOTE: contains filtered hidden or unexported fields
348+
}
349+
```
350+
A PCG is a PCG generator with 128 bits of internal state\. A zero PCG is equivalent to NewPCG\(0, 0\)\.
351+
352+
### Implemented Traits
353+
354+
- `Source`
355+
356+
### Seed
357+
```jule
358+
fn Seed(*self, seed1: u64, seed2: u64)
359+
```
360+
Resets the PCG to behave the same way as NewPCG\(seed1, seed2\)\.
361+
362+
### U64
363+
```jule
364+
fn U64(*self): u64
365+
```
366+
Return a uniformly\-distributed random u64 value\.

0 commit comments

Comments
 (0)