-1

I want to generate uniform random numbers in Node.js with the largest range possible. What's the best way to go about doing this?

(By "largest possible" I mean something like 2^64 possibilities, not an infinite number of possibilities).

3
  • Number.MAX_VALUE*Math.random(); I'm not quite sure what do you really want to achieve. Commented Mar 30, 2016 at 1:44
  • Math.random() already has the max range of a float in Javascript. If you want, you can remove the decimal point, but either way it's still max range. To get more range than that, you would need some other type of number (not a native number type in JS). You can spin a random set of digits or character in a string as long as you'd like. Commented Mar 30, 2016 at 1:47
  • 1
    What counts as “like” 2^64 possibilities? And do you mean integers specifically, or…? (You can get any range with a BigInt, or 2^52 on V8 with a single Math.random() call (which is why both existing answers are wrong, especially for Node).) Commented Apr 29, 2024 at 19:29

2 Answers 2

0

You can use the Number object's MAX_SAFE_INTEGER property, which was introduced in ES6 (there's also a polyfill for older versions of Node here) and gives the largest integer that will not collide with any other as the underlying numerical representation system loses accuracy. If, for whatever reason, you don't care about collisions, you could use the much larger MAX_VALUE property on the same object. This is what I'd do:

// function declaration using ES6 arrow/lambda syntax
var getMaxRandomInt = () => Math.floor(Number.MAX_SAFE_INTEGER * Math.random());
// equivalent to the following:
// function getMaxRandomInt() { return Math.floor(Number.MAX_SAFE_INTEGER * Math.random()); }

// example
document.write(getMaxRandomInt());

Note that, though the distribution is uniform, most results will contain the same number of digits (or the same exponent, in scientific form) as the upper bound, given that there are many more large values than small. If you want to return a float, then you can just remove the Math.floor function (though, functionally, there won't be much difference with large values, especially since all numbers are ultimately stored as floats in JavaScript).

Sign up to request clarification or add additional context in comments.

4 Comments

I completely disagree with the statement that "most results will be clustered around the upper end" because the numbers are distributed uniformally (e.g. every number appears with the same probability). It would be better to say that the results are most likely going to contain large numbers.
@MokonaModoki Okay, I'll reword that statement then. The fact remains that the result will more likely be greater than 1e15, for example, than not, and thus the probability of a number seemingly close to the upper bound being returned is high (or, put better, with the same number of digits), which is what I was trying to get across. Even though there is a uniform distribution. Thanks for pointing it out anyway.
How is this uniform if only odd numbers are returned?
@conical Simply use Math.ceil instead of floor?
0

Slight modification of @user162097's answer

var getRandomIntWithLargestRange = () => Math.floor(
  Number.MAX_SAFE_INTEGER * (2 * (Math.random() - 0.5))
);

// example
document.write(getRandomIntWithLargestRange());

1 Comment

Signed, nice! Though, of course, you can also use my function and conditionally modify the result (since Number.MIN_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER): var r = getMaxRandomInt(); if (Math.random() < .5) r *= -1;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.