Total Complexity | 2 |
Complexity/F | 1 |
Lines of Code | 21 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /* eslint-disable no-magic-numbers */ |
||
2 | import crypto from 'crypto'; |
||
3 | import Generator from './Base'; |
||
4 | |||
5 | /** |
||
6 | * Generates a random number from 0 to 1. |
||
7 | * @returns {number} number |
||
8 | * @generator |
||
9 | */ |
||
10 | export default class RandomGenerator extends Generator { |
||
11 | generate() { |
||
12 | const bytes = crypto.randomBytes(7); |
||
13 | |||
14 | let randomValue = (bytes[0] % (2 ** 5)) / (2 ** 5); |
||
15 | |||
16 | bytes.slice(1).forEach(byte => { |
||
17 | randomValue = (randomValue + byte) / (2 ** 8); |
||
18 | }); |
||
19 | |||
20 | return randomValue; |
||
21 | } |
||
22 | } |
||
23 |