The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Your best snippets Reply To: Your best snippets


Return a number 1-n with javascript:

number = Math.floor((Math.random() * n) + 1);

n can be any number, so if you wanted to simulate a 6-sided die roll, it would be a 6.
Pretty basic, but I use it all the time.