Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Last active March 29, 2023 14:58
Show Gist options
  • Save IvanAdmaers/7f05fb4c35c711c485521a9f31029167 to your computer and use it in GitHub Desktop.
Save IvanAdmaers/7f05fb4c35c711c485521a9f31029167 to your computer and use it in GitHub Desktop.
Random Roulette Bet | NodeJS | Using crypto (not unsafety Math.random)
const randomRouletteWinBet = () => {
// The possible win bet numbers on an American roulette wheel
const possibleWinBets = [
'0',
'00',
...Array.from({length: 36}, (_, i) => String(i+1))
];
// Generate a cryptographically secure random index
const randomIndex = window.crypto.getRandomValues(new Uint32Array(1))[0] % possibleWinBets.length;
// Return the corresponding win bet number
return possibleWinBets[randomIndex];
};
console.log(randomRouletteWinBet());
import crypto from 'crypto';
const randomRouletteWinBet = () => {
// The possible win bet numbers on an American roulette wheel
const possibleWinBets = [
'0',
'00',
...Array.from({ length: 36 }, (_, i) => String(i + 1)),
];
// Generate a cryptographically secure random index
const randomIndex = crypto.randomInt(possibleWinBets.length);
// Return the corresponding win bet number
return possibleWinBets[randomIndex];
};
console.log(randomRouletteWinBet());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment