Skip to content

Commit

Permalink
⚡️ Faster Congruencial 32bits (#512)
Browse files Browse the repository at this point in the history
* ⚡️ Faster Congruencial 32bits

* Update LinearCongruential.ts
  • Loading branch information
dubzzz authored Jan 8, 2023
1 parent 0bde03e commit b4852a8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/generator/LinearCongruential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const computeValueFromNextSeed = function (nextseed: number) {
};

class LinearCongruential32 implements RandomGenerator {
static readonly min: number = 0;
static readonly max: number = 0xffffffff;
static readonly min: number = -0x80000000;
static readonly max: number = 0x7fffffff;

constructor(private seed: number) {}

Expand Down Expand Up @@ -52,7 +52,7 @@ class LinearCongruential32 implements RandomGenerator {
// but as binary operations truncate between -0x80000000 and 0x7fffffff in JavaScript
// we can get rid of this operation
const vnext = v3 + ((v2 + (v1 << 15)) << 15);
return ((vnext + 0x80000000) | 0) + 0x80000000;
return vnext | 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/generator/LinearCongruencial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ describe('congruential32', () => {
100594764, 1754357691, 2940213637, 1822860290, 1822569888, 3692081352, 4126111909, 2548328615, 1559153086,
2229736813, 3416000363, 1307522393, 2537287304, 1493158878, 3075976674, 3907402638, 433041229, 3014088443,
3259785341, 1539470501, 1803261200, 518157909, 1135233158, 406515323, 3831259675, 2405854817, 1438184902,
]
].map((v) => v | 0)
);
});
it('Should return the same sequence given same seeds', () => fc.assert(p.sameSeedSameSequences(congruential32)));
it('Should return the same sequence if called twice', () => fc.assert(p.sameSequencesIfCallTwice(congruential32)));
it('Should generate values between 0 and 2**32 -1', () => fc.assert(p.valuesInRange(congruential32)));
it('Should generate values between -2**31 and 2**31 -1', () => fc.assert(p.valuesInRange(congruential32)));
it('Should impact itself with unsafeNext', () => fc.assert(p.changeSelfWithUnsafeNext(congruential32)));
it('Should not impact itself with next', () => fc.assert(p.noChangeSelfWithNext(congruential32)));
it('Should not impact clones when impacting itself on unsafeNext', () =>
Expand Down

0 comments on commit b4852a8

Please sign in to comment.