Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created February 27, 2014 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesbos/9253844 to your computer and use it in GitHub Desktop.
Save wesbos/9253844 to your computer and use it in GitHub Desktop.
// Why do we use:
Math.floor(Math.random() * 6) + 1;
// vs
Math.ceil(Math.random() * 6);
@zouloux
Copy link

zouloux commented Feb 27, 2014

Maybe because Math.ceil(0) == 0 and Math.floor(0) + 1 == 1 ? Just 2 different things :)

@peterc
Copy link

peterc commented Feb 27, 2014

Yeah, the latter could potentially be 0 very rarely, when you wanted 1 through 6.

I just made an app that ran 100 billion times and Math.random was never 0 but formally it could be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment