-
-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deterministic key generation using pseudo randomness (#1033)
* deterministic key generation using pseudo randomness * linter on the psuedo random commit
- Loading branch information
1 parent
07bf247
commit adeb734
Showing
14 changed files
with
76 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Generates a pseudo random number. Although it is not cryptographically secure | ||
* and uniformly distributed, it is not a concern for the intended use-case, | ||
* which is to generate distinct numbers. | ||
* | ||
* Credit: https://stackoverflow.com/a/19303725/10254049 | ||
*/ | ||
export class SimpleRNG { | ||
private seed = 1; | ||
|
||
constructor(seed: number) { | ||
this.seed = seed; | ||
} | ||
|
||
nextInt = () => { | ||
const x = Math.sin(this.seed++) * 10000; | ||
return x - Math.floor(x); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SimpleRNG } from 'src/utils/rng'; | ||
|
||
describe(`psuedo random numbers`, () => { | ||
it(`generates distinct numbers`, () => { | ||
const rng = new SimpleRNG(1); | ||
expect(rng.nextInt()).not.toEqual(rng.nextInt()); | ||
}); | ||
|
||
it(`generates the same number across different SimpleRNG`, () => { | ||
const rng = new SimpleRNG(1); | ||
expect(rng.nextInt()).toEqual(0.7098480789645691); | ||
expect(rng.nextInt()).toEqual(0.9742682568175951); | ||
}); | ||
}); |