Skip to content

Commit

Permalink
feat: 🎸 add xosrshift implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 13, 2024
1 parent 1b74176 commit e32a638
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/xorshift.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {xorShift32} from '../xorshift';

test('generates random integers', () => {
const numbers: number[] = [];
for (let i = 0; i < 100; i++) {
const num = xorShift32();
numbers.push(num);
expect(Math.floor(num)).toBe(num);
}
const set = new Set(numbers);
expect(set.size).toBe(100);
});
11 changes: 11 additions & 0 deletions src/xorshift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const makeXorShift32 = (seed: number = 1 + Math.round(Math.random() * ((-1>>>0)-1))) => {
let x = seed|0;
return function xorShift32() {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
};
}

export const xorShift32 = makeXorShift32();

0 comments on commit e32a638

Please sign in to comment.