forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.ts
More file actions
30 lines (23 loc) · 796 Bytes
/
random.ts
File metadata and controls
30 lines (23 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as crypto from 'crypto';
import { injectable } from 'inversify';
import { IRandom } from '../types';
function getRandom(): number {
let num: number = 0;
const buf: Buffer = crypto.randomBytes(2);
num = (buf.readUInt8(0) << 8) + buf.readUInt8(1);
const maxValue: number = Math.pow(16, 4) - 1;
return num / maxValue;
}
function getRandomBetween(min: number = 0, max: number = 10): number {
const randomVal: number = getRandom();
return min + randomVal * (max - min);
}
@injectable()
export class Random implements IRandom {
public getRandomInt(min: number = 0, max: number = 10): number {
return getRandomBetween(min, max);
}
}