@internal
| 91 | |
| 92 | /** @internal */ |
| 93 | class FixedRandomImpl implements Random.Random { |
| 94 | readonly [RandomTypeId]: Random.RandomTypeId = RandomTypeId |
| 95 | |
| 96 | private index = 0 |
| 97 | |
| 98 | constructor(readonly values: Arr.NonEmptyArray<any>) { |
| 99 | if (values.length === 0) { |
| 100 | throw new Error("Requires at least one value") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private getNextValue(): any { |
| 105 | const value = this.values[this.index] |
| 106 | this.index = (this.index + 1) % this.values.length |
| 107 | return value |
| 108 | } |
| 109 | |
| 110 | get next(): Effect.Effect<number> { |
| 111 | return core.sync(() => { |
| 112 | const value = this.getNextValue() |
| 113 | if (typeof value === "number") { |
| 114 | return Math.max(0, Math.min(1, value)) |
| 115 | } |
| 116 | return Hash.hash(value) / 2147483647 |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | get nextBoolean(): Effect.Effect<boolean> { |
| 121 | return core.sync(() => { |
| 122 | const value = this.getNextValue() |
| 123 | if (typeof value === "boolean") { |
| 124 | return value |
| 125 | } |
| 126 | return Hash.hash(value) % 2 === 0 |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | get nextInt(): Effect.Effect<number> { |
| 131 | return core.sync(() => { |
| 132 | const value = this.getNextValue() |
| 133 | if (typeof value === "number" && Number.isFinite(value)) { |
| 134 | return Math.round(value) |
| 135 | } |
| 136 | return Math.abs(Hash.hash(value)) |
| 137 | }) |
| 138 | } |
| 139 | |
| 140 | nextRange(min: number, max: number): Effect.Effect<number> { |
| 141 | return core.map(this.next, (n) => (max - min) * n + min) |
| 142 | } |
| 143 | |
| 144 | nextIntBetween(min: number, max: number): Effect.Effect<number> { |
| 145 | return core.sync(() => { |
| 146 | const value = this.getNextValue() |
| 147 | if (typeof value === "number" && Number.isFinite(value)) { |
| 148 | return Math.max(min, Math.min(max - 1, Math.round(value))) |
| 149 | } |
| 150 | const hash = Math.abs(Hash.hash(value)) |
nothing calls this directly
no outgoing calls
no test coverage detected