mirror of
https://github.com/billsonnn/nitro-react.git
synced 2024-11-27 16:10:50 +01:00
29 lines
724 B
TypeScript
29 lines
724 B
TypeScript
|
export class Randomizer
|
||
|
{
|
||
|
public static getRandomNumber(count: number): number
|
||
|
{
|
||
|
return Math.floor(Math.random() * count);
|
||
|
}
|
||
|
|
||
|
public static getRandomElement<T>(elements: T[]): T
|
||
|
{
|
||
|
return elements[this.getRandomNumber(elements.length)];
|
||
|
}
|
||
|
|
||
|
public static getRandomElements<T>(elements: T[], count: number): T[]
|
||
|
{
|
||
|
const result: T[] = new Array(count);
|
||
|
let len = elements.length;
|
||
|
const taken = new Array(len);
|
||
|
|
||
|
while(count--)
|
||
|
{
|
||
|
var x = this.getRandomNumber(len);
|
||
|
result[count] = elements[x in taken ? taken[x] : x];
|
||
|
taken[x] = --len in taken ? taken[len] : len;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
}
|