[Home] > Snippets  > Languages  > JavaScript  > Misc  >  Emulate a dice throw

Emulate a dice throw

JavaScript

const throwdice = () => ~~(Math.random() * 6) + 1

TypeScript

const throwdice = (): number => ~~(Math.random() * 6) + 1

Examples

throwdice() // 4
throwdice() // 1
throwdice() // 6