32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
export const getRandomIntInclusive = (min: number, max: number) => {
|
||
const minCeiled = Math.ceil(min);
|
||
const maxFloored = Math.floor(max);
|
||
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive
|
||
}
|
||
export const shuffle_array = (array: any[]) => {
|
||
let currentIndex = array.length;
|
||
|
||
// While there remain elements to shuffle...
|
||
while (currentIndex != 0) {
|
||
|
||
// Pick a remaining element...
|
||
let randomIndex = Math.floor(Math.random() * currentIndex);
|
||
currentIndex--;
|
||
|
||
// And swap it with the current element.
|
||
[array[currentIndex], array[randomIndex]] = [
|
||
array[randomIndex], array[currentIndex]];
|
||
}
|
||
return array
|
||
}
|
||
|
||
export const random_сolor = () => {
|
||
const r = () => Math.floor(200 * Math.random());
|
||
|
||
return `rgb(${r()}, ${r()}, ${r()})`;
|
||
}
|
||
export function* chunks<T>(arr: T[], n: number): Generator<T[], void> {
|
||
for (let i = 0; i < arr.length; i += n) {
|
||
yield arr.slice(i, i + n);
|
||
}
|
||
} |