Skip to content

Commit cdf7625

Browse files
committed
Add more docs for random number generators
1 parent feb8b62 commit cdf7625

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

.swiftlint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ disabled_rules:
55
- identifier_name
66
- file_length
77
- type_body_length
8+
- large_tuple
9+
- legacy_random
810

911
excluded:
1012
- .build

Sources/Numerix/RandomModule/Wyrand.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
2-
Wyrand pseudorandom number generator (PRNG) based on wyrand and wyhash functions by Wang Yi.
2+
Wyrand pseudorandom number generator (PRNG).
3+
Based on the wyrand and wyhash functions by Wang Yi.
34

4-
Original code for wyrand and wyhash functions
5+
References:
56
https://github.com/wangyi-fudan/wyhash
6-
7-
Also see Daniel Lemire's article about wyhash
87
https://lemire.me/blog/2019/03/19/the-fastest-conventional-random-number-generator-that-can-pass-big-crush/
98
*/
109

Sources/Numerix/RandomModule/Xoroshiro.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/*
22
Xoroshiro128+ and Xoroshiro128++ pseudorandom number generators (PRNGs).
3+
4+
References:
5+
https://prng.di.unimi.it
6+
https://prng.di.unimi.it/xoroshiro128plus.c
37
*/
48

59
import Accelerate
@@ -8,16 +12,20 @@ func rotl(_ x: UInt64, _ k: Int) -> UInt64 {
812
(x &<< k) | (x &>> (64 &- k))
913
}
1014

11-
/// Xoroshiro128+ is a 64-bit pseudorandom number generator (PRNG) for double-precision values.
15+
/// Xoroshiro128+ is a 64-bit pseudorandom number generator (PRNG) for floating-point numbers.
1216
public struct Xoroshiro128Plus: RandomNumberGenerator {
1317
private var state: (UInt64, UInt64)
1418

19+
/// Initialize the state of the generator.
20+
/// - Parameter seed: Seed with two 64-bit unsigned integers.
1521
public init(seed: (UInt64, UInt64)? = nil) {
1622
let a = UInt64.random(in: 0..<UInt64.max)
1723
let b = UInt64.random(in: 0..<UInt64.max)
1824
state = seed ?? (a, b)
1925
}
2026

27+
/// Generate a random 64-bit unsigned integer.
28+
/// - Returns: Random 64-bit unsigned integer value.
2129
public mutating func next() -> UInt64 {
2230
let s0 = state.0
2331
var s1 = state.1
@@ -30,24 +38,28 @@ public struct Xoroshiro128Plus: RandomNumberGenerator {
3038
return result
3139
}
3240

33-
/// Generate a random double-precision value from a uniform distribution over [0, 1) which includes zero but
41+
/// Generate a random double-precision value from a uniform distribution in [0, 1) which includes zero but
3442
/// excludes one.
3543
/// - Returns: Random double-precision value.
3644
public mutating func next() -> Double {
3745
Double(next() >> 11) * 0x1.0p-53
3846
}
3947
}
4048

41-
/// Xoroshiro128++ is a 64-bit pseudorandom number generator (PRNG) for double-precision values.
49+
/// Xoroshiro128++ is a 64-bit pseudorandom number generator (PRNG) for floating-point numbers.
4250
public struct Xoroshiro128PlusPlus: RandomNumberGenerator {
4351
private var state: (UInt64, UInt64)
4452

53+
/// Initialize the state of the generator.
54+
/// - Parameter seed: Seed with two 64-bit unsigned integers.
4555
public init(seed: (UInt64, UInt64)? = nil) {
4656
let a = UInt64.random(in: 0..<UInt64.max)
4757
let b = UInt64.random(in: 0..<UInt64.max)
4858
state = seed ?? (a, b)
4959
}
5060

61+
/// Generate a random 64-bit unsigned integer.
62+
/// - Returns: Random 64-bit unsigned integer value.
5163
public mutating func next() -> UInt64 {
5264
let s0 = state.0
5365
var s1 = state.1
@@ -59,8 +71,8 @@ public struct Xoroshiro128PlusPlus: RandomNumberGenerator {
5971

6072
return result
6173
}
62-
63-
/// Generate a random double-precision value from a uniform distribution over [0, 1) which includes zero but
74+
75+
/// Generate a random double-precision value from a uniform distribution in [0, 1) which includes zero but
6476
/// excludes one.
6577
/// - Returns: Random double-precision value.
6678
public mutating func next() -> Double {

Sources/Numerix/RandomModule/Xoshiro.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/*
22
Xoshiro128+ pseudorandom number generator (PRNG).
3+
4+
References:
5+
https://prng.di.unimi.it
6+
https://prng.di.unimi.it/xoshiro128plus.c
37
*/
48

59
import Accelerate
610

7-
/// Xoshiro128+ is a 32-bit pseudorandom number generator (PRNG) for single-precision (floating point) values.
11+
/// Xoshiro128+ is a 32-bit pseudorandom number generator (PRNG) for single-precision (32-bit floating point) numbers.
812
public struct Xoshiro128Plus {
913
private var state: (UInt32, UInt32, UInt32, UInt32)
1014

15+
/// Initialize the state of the generator.
16+
/// - Parameter seed: Seed with four 32-bit unsigned integers.
1117
public init(seed: (UInt32, UInt32, UInt32, UInt32)? = nil) {
1218
state = seed ?? (arc4random(), arc4random(), arc4random(), arc4random())
1319
}
1420

21+
/// Generate a random 32-bit unsigned integer.
22+
/// - Returns: Random 32-bit unsigned integer value.
1523
public mutating func next() -> UInt32 {
1624
let result = state.0 &+ state.3
1725
let t = state.1 << 9
@@ -28,7 +36,7 @@ public struct Xoshiro128Plus {
2836
return result
2937
}
3038

31-
/// Generate a random single-precision value from a uniform distribution over [0, 1) which includes zero but
39+
/// Generate a random single-precision value from a uniform distribution in [0, 1) which includes zero but
3240
/// excludes one.
3341
/// - Returns: Random single-precision value.
3442
public mutating func next() -> Float {

0 commit comments

Comments
 (0)