Files
zipline/src/lib/random.ts
dicedtomato b767a0082e Merge commit from fork
* fix: math.random not random

* fix: ignore require() warnings
2025-03-19 14:42:13 -07:00

30 lines
963 B
TypeScript
Executable File

const CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const CHARSET_LENGTH = CHARSET.length;
export function randomCharacters(length: number) {
const randomValues = new Uint8Array(length);
typeof crypto !== 'undefined' && crypto.getRandomValues
? crypto.getRandomValues(randomValues)
: // eslint-disable-next-line @typescript-eslint/no-require-imports
require('crypto').webcrypto.getRandomValues(randomValues);
let result = '';
for (let i = 0; i < length; i++) {
result += CHARSET[randomValues[i] % CHARSET_LENGTH];
}
return result;
}
export function randomIndex(length: number) {
const randomValues = new Uint8Array(1);
typeof crypto !== 'undefined' && crypto.getRandomValues
? crypto.getRandomValues(randomValues)
: // eslint-disable-next-line @typescript-eslint/no-require-imports
require('crypto').webcrypto.getRandomValues(randomValues);
return randomValues[0] % length;
}