forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomSequence.swift
More file actions
32 lines (28 loc) · 808 Bytes
/
RandomSequence.swift
File metadata and controls
32 lines (28 loc) · 808 Bytes
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
//
// RandomSequence.swift
// ShapeScript
//
// Created by Nick Lockwood on 07/11/2018.
// Copyright © 2018 Nick Lockwood. All rights reserved.
//
private extension Double {
static let modulus: Double = .init(UInt32.max) + 1
static let multiplier: Double = 1664525
static let increment: Double = 1013904223
}
final class RandomSequence {
var seed: Double {
didSet {
seed = seed.truncatingRemainder(dividingBy: .modulus)
}
}
// create sequence with specific seed
init(seed: Double) {
self.seed = seed.truncatingRemainder(dividingBy: .modulus)
}
// compute next seed and return value as double in range 0.0 ..< 1.0
func next() -> Double {
seed = seed * .multiplier + .increment
return seed / .modulus
}
}