Files
Bubberstation/tgui/packages/common/random.ts
SkyratBot 332a74ea76 [MIRROR] Adds Prettierx - or how I broke TGUI for the nth time [MDB IGNORE] (#14475)
* Adds Prettierx - or how I broke TGUI for the nth time

* fuck

Co-authored-by: Jeremiah <42397676+jlsnow301@users.noreply.github.com>
Co-authored-by: Gandalf <9026500+Gandalf2k15@users.noreply.github.com>
2022-06-25 01:17:32 +01:00

33 lines
942 B
TypeScript

import { clamp } from './math';
/**
* Returns random number between lowerBound exclusive and upperBound inclusive
*/
export const randomNumber = (lowerBound: number, upperBound: number) => {
return Math.random() * (upperBound - lowerBound) + lowerBound;
};
/**
* Returns random integer between lowerBound exclusive and upperBound inclusive
*/
export const randomInteger = (lowerBound: number, upperBound: number) => {
lowerBound = Math.ceil(lowerBound);
upperBound = Math.floor(upperBound);
return Math.floor(Math.random() * (upperBound - lowerBound) + lowerBound);
};
/**
* Returns random array element
*/
export const randomPick = <T>(array: T[]) => {
return array[Math.floor(Math.random() * array.length)];
};
/**
* Return 1 with probability P percent; otherwise 0
*/
export const randomProb = (probability: number) => {
const normalized = clamp(probability, 0, 100) / 100;
return Math.random() <= normalized;
};