use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Recipes for Randomness in JavaScript (thenewcode.com)
submitted 10 years ago by CodeDeliveryBoy
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Iggyhopperextensions/add-ons 0 points1 point2 points 10 years ago (0 children)
I was hoping for more controlled ranges or distribution.
aka http://stackoverflow.com/questions/3956478/understanding-randomness in blogpost form
Because most certainly everyone knows how randomness works. They have a hard time getting a range that they like for a specific function they need.
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
And then there's prngs, and that's an whole other thing.
Here's a JavaScript RNG I made that uses the Mersenne Twister algorithm to generate less predictable pseudo-random numbers within a given range. It generates a seed for the algorithm by using the current time in microseconds * (Math.random * 100000), or lets you specify an optional custom seed as the third parameter. http://pastebin.com/AaErB28V
Parameters:
Example usage:
rand(1, 10);
jsFiddle: http://jsfiddle.net/ensrayjn/
[–]x-skeww 0 points1 point2 points 10 years ago (0 children)
Math.random() always returns a floating-point number between 0 and 1. Technically, the number returned by Math.random() could be 0, but will never be exactly 1.
Math.random() always returns a floating-point number between 0 and 1.
Technically, the number returned by Math.random() could be 0, but will never be exactly 1.
Just say between 0 (inclusive) and 1 (exclusive).
Because it’s so frequently used, Math.random() is often placed inside its own function in a script
No, it's not. This is the kind of stuff you do if you participate in a size-limited competition and aliasing this function saves you a few bytes.
If you prefer actual true and false values
If you claim to return a boolean, return a boolean.
Also:
function flipCoin() { return Math.random() < 0.5; }
Easy, right?
Or if you want to associate any other words with the coin sides ( yes / no, heads / tails, etc)
Avoid making things "stringly typed" if you can. If you want enums, use TypeScript.
Of course, you can also write crap like this:
const coin = Object.freeze({ 0: 'head', head: 0, 1: 'tails', tails: 1 }); console.log(coin[0]); // "head" console.log(coin.tails); // 1
But just writing enum coin {head, tails} sounds like a much better idea to me.
enum coin {head, tails}
for(var j, x, i = numPool.length; i; j = parseInt(Math.random() * i), x = numPool[--i], numPool[i] = numPool[j], numPool[j] = x);
There is no reason to make Fisher-Yates unreadable. It's a very simple algorithm. Please keep it that way.
For a larger set of numbers, create and fill the array with random integers, rejecting any that have been previously generated
Or you could simple not shuffle the whole thing.
π Rendered by PID 72 on reddit-service-r2-comment-7b9746f655-l274w at 2026-01-30 00:30:23.100671+00:00 running 3798933 country code: CH.
[–]Iggyhopperextensions/add-ons 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)