Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New prng #552

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/engine/PRNG.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
// Taken from https://gist.github.com/blixt/f17b47c62508be59987b
// Taken from https://github.com/bryc/code/blob/master/jshash/PRNGs.md
// Ink uses a seedable PRNG of which there is none in native javascript.
export class PRNG {
private seed: number;

constructor(seed: number) {
this.seed = seed % 2147483647;
if (this.seed <= 0) this.seed += 2147483646;
this.seed = seed;
}

public nextSeed(): number {
let a = this.seed;
a |= 0;
a = (a + 0x6d2b79f5) | 0;
this.seed = a;
return a;
}
public next(): number {
return (this.seed = (this.seed * 16807) % 2147483647);
let a = this.seed;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return (t ^ (t >>> 14)) >>> 0;
}
public nextFloat(): number {
return (this.next() - 1) / 2147483646;
return this.next() / 4294967296;
}
}
15 changes: 8 additions & 7 deletions src/engine/Story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,15 @@ export class Story extends InkObject {
". The maximum must be larger"
);

let resultSeed = this.state.storySeed + this.state.previousRandom;
let resultSeed = this.state.previousRandom;
let random = new PRNG(resultSeed);

let nextRandom = random.next();
let chosenValue = (nextRandom % randomRange) + minInt.value;
this.state.PushEvaluationStack(new IntValue(chosenValue));

// Next random number (rather than keeping the Random object around)
this.state.previousRandom = nextRandom;
this.state.previousRandom = random.nextSeed();
break;
}

Expand All @@ -1212,8 +1212,7 @@ export class Story extends InkObject {
return throwNullException("minInt.value");
}

this.state.storySeed = seed.value;
this.state.previousRandom = 0;
this.state.previousRandom = seed.value;

this.state.PushEvaluationStack(new Void());
break;
Expand Down Expand Up @@ -1349,7 +1348,7 @@ export class Story extends InkObject {
newList = new InkList();
} else {
// Generate a random index for the element to take
let resultSeed = this.state.storySeed + this.state.previousRandom;
let resultSeed = this.state.previousRandom;
let random = new PRNG(resultSeed);

let nextRandom = random.next();
Expand Down Expand Up @@ -1377,7 +1376,7 @@ export class Story extends InkObject {
newList = new InkList(randomItem.Key.originName, this);
newList.Add(randomItem.Key, randomItem.Value);

this.state.previousRandom = nextRandom;
this.state.previousRandom = random.nextSeed();
}

this.state.PushEvaluationStack(new ListValue(newList));
Expand Down Expand Up @@ -2092,7 +2091,9 @@ export class Story extends InkObject {
}

for (let i = 0; i <= iterationIndex; ++i) {
let chosen = random.next() % unpickedIndices.length;
let nextRandom = random.next();
let chosen = nextRandom % unpickedIndices.length;
random.nextSeed();
let chosenIndex = unpickedIndices[chosen];
unpickedIndices.splice(chosen, 1);

Expand Down
6 changes: 3 additions & 3 deletions src/engine/StoryState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ export class StoryState {
this._turnIndices = new Map();
this.currentTurnIndex = -1;

let timeSeed = new Date().getTime();
this.storySeed = new PRNG(timeSeed).next() % 100;
this.previousRandom = 0;
let timeSeed = Date.now();
this.storySeed = new PRNG(timeSeed).nextSeed();
this.previousRandom = this.storySeed;

this._currentChoices = [];

Expand Down