-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1036 from Hopding/1033-cleanup
Deterministic key generation using pseudo randomness (tweaks)
- Loading branch information
Showing
13 changed files
with
79 additions
and
60 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,21 @@ | ||
/** | ||
* 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 { | ||
static withSeed = (seed: number) => new SimpleRNG(seed); | ||
|
||
private seed: number; | ||
|
||
private constructor(seed: number) { | ||
this.seed = seed; | ||
} | ||
|
||
nextInt(): number { | ||
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
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 = SimpleRNG.withSeed(1); | ||
expect(rng.nextInt()).not.toEqual(rng.nextInt()); | ||
}); | ||
|
||
it(`generates the same number across different SimpleRNG`, () => { | ||
const rng = SimpleRNG.withSeed(1); | ||
expect(rng.nextInt()).toEqual(0.7098480789645691); | ||
expect(rng.nextInt()).toEqual(0.9742682568175951); | ||
}); | ||
}); |