Created
January 30, 2020 10:58
-
-
Save nyawach/6e87acdfc2d2ffe35cffc1f5074e7306 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SeededRandom from "../SeededRandom" | |
describe("SeededRandom.ts", () => { | |
it("インスタンスが違っていても、シード値が同じなら同じ値が返ってくる", () => { | |
const seed = 100 | |
const r1 = new SeededRandom(seed) | |
const r2 = new SeededRandom(seed) | |
expect(r1.next()).toBe(r2.next()) | |
expect(r1.next()).toBe(r2.next()) | |
expect(r1.next()).toBe(r2.next()) | |
expect(r1.next()).toBe(r2.next()) | |
expect(r1.next()).toBe(r2.next()) | |
}) | |
it("シード値が別なら違う値が返ってくる", () => { | |
const r1 = new SeededRandom(100) | |
const r2 = new SeededRandom(101) | |
expect(r1.next()).not.toBe(r2.next()) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SeedeRandom | |
* シード指定可能なランダム変数 | |
* @ref https://qiita.com/hp0me/items/ec10a59d8e3a05d90c83 | |
*/ | |
export default class SeededRandom { | |
private seed: number | |
constructor(seed: number = 123456789) { | |
this.seed = seed | |
} | |
next(max: number = 1, min: number = 0) { | |
max = max || 1 | |
min = min || 0 | |
this.seed = (this.seed * 9301 + 49297) % 233280 | |
const rnd = this.seed / 233280 | |
return min + rnd * (max - min) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment