-
Notifications
You must be signed in to change notification settings - Fork 106
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
change the magic number 16807 to 16811 #947
Conversation
Update the assertion value under
|
Thanks for your contribution @blueloveTH ! 16807 is actually a number that's usually recommended as the |
@@ -155,7 +155,7 @@ describe("Logic", () => { | |||
it("should generate random numbers", () => { | |||
story.ChoosePathString("logic.random"); | |||
|
|||
expect(story.Continue()).toEqual("15\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- expect(story.Continue()).toEqual("15\n");
- expect(story.Continue()).toEqual("-24\n");
+ expect(story.Continue()).toEqual("27\n");
+ expect(story.Continue()).toEqual("8\n");
@@ -8,7 +8,7 @@ export class PRNG { | |||
if (this.seed <= 0) this.seed += 2147483646; | |||
} | |||
public next(): number { | |||
return (this.seed = (this.seed * 16807) % 2147483647); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- return (this.seed = (this.seed * 16807) % 2147483647);
+ return (this.seed = (this.seed * 48271) % 2147483647);
After further investigation :
which makes the seed low enough
So if that's ok with you @blueloveTH, I commented your code with the value expected for the tests with @y-lohse @ephread I think we could merge this one (because that's a quick fix) while work is being done in #552 |
Thanks for your investigation @smwhr ! |
Happy to merge this if we do the changes suggested by @smwhr , thanks for the investigation :) |
Closing this one in favour of #952 |
Checklist
npm test
).npm run-script lint
).Description
I detected a problem in the combination of
ink v1.0.0
andink.js v2.0.0
.If the length of a shuffle list is
7
, it will always return the first element.Such as:
which always get
1
anda
. However, the problem disappears for other lengths such as6
or8
. It made me confused.So I inferred that there is a magic number.
By checking the source, I identified it is
16807
.This number is a multiple of
7
.If
this.seed * 16807 < 2147483647
,PRNG.next() % 7
should always be zero.In the implmentation of
LIST_RANDOM
, it usesPRNG.next() % list.Count
.So I think this is why we always get the first element on
length == 7
.My solution is to replace
16807
with16811
. The latter is a prime.I tested on our usecases to confirm that this modification solves the magical problem.