You know that feeling – you just want to get a random number between 1 and 5.
So you do what every JavaScript dev has done a million times:
Math.floor(Math.random() * 5) + 1;
And while it works, it’s just… clunky. The logic is unintuitive to newcomers, the syntax feels dated, and worst of all — we all end up copy-pasting it from ChatGPT instead of actually remembering what it does.
It gets worse when you want something more nuanced:
Want a random boolean? You’ll end up doing Math.random() > 0.5.
Want to pick a random item from an array? It’s arr[Math.floor(Math.random() * arr.length)].
Want cryptographically secure randomness? Hello crypto.getRandomValues() and extra boilerplate.
JavaScript deserved better.
And now, it just might get it.
Introducing the new Random object (TC39 Proposal)
The TC39 proposal for Random functions is aiming to bring a modern, flexible, and developer-friendly way to handle randomness in JavaScript.
Think of it as Math.random() on steroids — but cleaner, clearer, and more powerful.
So what’s in the box?
Let’s take a peek at what this proposal brings:
Random.int(min, max)
A simple, readable way to get an inclusive random integer between two values:
Random.int(1, 5); // could be 1, 2, 3, 4, or 5
No more manual flooring and adjusting!
Random.float(min, max)
Get a floating-point number between min and max:
Random.float(0, 1); // behaves like Math.random(), but scoped!
Random.boolean()
Flip a coin — easily.
Random.boolean(); // true or false
Random.pick(array)
Pick a random item from an array — finally built-in:
Random.pick(["red", "green", "blue"]);
Random.shuffle(array)
Need a shuffled version of an array? It's just one call away:
Random.shuffle([1, 2, 3, 4, 5]);
// might return [3, 1, 5, 2, 4]
Pluggable RNGs (even secure ones!)
Want cryptographic randomness? Or reproducible random numbers (useful in tests or games)?
The new API supports custom generators:
const rng = Random.withSeed(12345);
rng.int(1, 10); // same output every time for same seed!
Or plug in a secure RNG source for critical applications.
Why not just extend Math.random()?
The new Random object is separate by design. It allows:
- Multiple independent RNGs with different behaviors (e.g., seeded vs. secure)
- Cleaner APIs without polluting the global Math object
- Better support for deterministic or cryptographic scenarios
- It’s a modern solution for modern needs.
Current status?
This is still a stage 2 proposal in the TC39 process — meaning it’s being actively worked on and refined, but it’s not part of the JavaScript standard yet.
You can track progress and contribute feedback here:
https://github.com/tc39/proposal-random-functions
For years, JavaScript devs have relied on hacks, one-liners, and utility libraries just to get the basics done. This proposal has the potential to change that forever.
If you’ve ever written Math.floor(Math.random() * (max - min + 1)) + min
, you’ll immediately see the value here.
And honestly?
It’s about fu*king time.
Top comments (0)