Last active
March 29, 2023 14:58
-
-
Save IvanAdmaers/7f05fb4c35c711c485521a9f31029167 to your computer and use it in GitHub Desktop.
Random Roulette Bet | NodeJS | Using crypto (not unsafety Math.random)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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