Skip to content

Commit cfc70bb

Browse files
committed
Update PRNG logic
The new algorithm, Mulberry32, doesn't return its internal state as the generated random number. This means that previousRandom, the value we keep between generations, should not be the actual previous random number generated, but the previous seed normally kept inside the generator. To do that, a new nextSeed() method is added. Also, the storySeed is no longer fed into the PRNG, but only used on first initialization.
1 parent e2d9cae commit cfc70bb

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/engine/Story.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1189,15 +1189,15 @@ export class Story extends InkObject {
11891189
". The maximum must be larger"
11901190
);
11911191

1192-
let resultSeed = this.state.storySeed + this.state.previousRandom;
1192+
let resultSeed = this.state.previousRandom;
11931193
let random = new PRNG(resultSeed);
11941194

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

11991199
// Next random number (rather than keeping the Random object around)
1200-
this.state.previousRandom = nextRandom;
1200+
this.state.previousRandom = random.nextSeed();
12011201
break;
12021202
}
12031203

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

1215-
this.state.storySeed = seed.value;
1216-
this.state.previousRandom = 0;
1215+
this.state.previousRandom = seed.value;
12171216

12181217
this.state.PushEvaluationStack(new Void());
12191218
break;
@@ -1349,7 +1348,7 @@ export class Story extends InkObject {
13491348
newList = new InkList();
13501349
} else {
13511350
// Generate a random index for the element to take
1352-
let resultSeed = this.state.storySeed + this.state.previousRandom;
1351+
let resultSeed = this.state.previousRandom;
13531352
let random = new PRNG(resultSeed);
13541353

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

1380-
this.state.previousRandom = nextRandom;
1379+
this.state.previousRandom = random.nextSeed();
13811380
}
13821381

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

20942093
for (let i = 0; i <= iterationIndex; ++i) {
2095-
let chosen = random.next() % unpickedIndices.length;
2094+
let nextRandom = random.next();
2095+
let chosen = nextRandom % unpickedIndices.length;
2096+
random.nextSeed();
20962097
let chosenIndex = unpickedIndices[chosen];
20972098
unpickedIndices.splice(chosen, 1);
20982099

0 commit comments

Comments
 (0)