<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nir Tzezana</title>
    <description>The latest articles on DEV Community by Nir Tzezana (@nirtz89).</description>
    <link>https://dev.to/nirtz89</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2541060%2Fc3522c45-5160-4d38-9632-74a1c44db4d6.jpg</url>
      <title>DEV Community: Nir Tzezana</title>
      <link>https://dev.to/nirtz89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nirtz89"/>
    <language>en</language>
    <item>
      <title>JavaScript's Random is about to get a lot better</title>
      <dc:creator>Nir Tzezana</dc:creator>
      <pubDate>Sun, 29 Jun 2025 15:16:32 +0000</pubDate>
      <link>https://dev.to/nirtz89/javascripts-random-is-about-to-get-better-1bma</link>
      <guid>https://dev.to/nirtz89/javascripts-random-is-about-to-get-better-1bma</guid>
      <description>&lt;p&gt;You know that feeling – you just want to get a random number between 1 and 5.&lt;/p&gt;

&lt;p&gt;So you do what every JavaScript dev has done a million times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;It gets worse when you want something more nuanced:&lt;/p&gt;

&lt;p&gt;Want a random boolean? You’ll end up doing Math.random() &amp;gt; 0.5.&lt;/p&gt;

&lt;p&gt;Want to pick a random item from an array? It’s &lt;code&gt;arr[Math.floor(Math.random() * arr.length)].&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Want cryptographically secure randomness? Hello crypto.getRandomValues() and extra boilerplate.&lt;/p&gt;

&lt;p&gt;JavaScript deserved better.&lt;/p&gt;

&lt;p&gt;And now, it just might get it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Introducing the new Random object (TC39 Proposal)&lt;/em&gt;&lt;br&gt;
The TC39 proposal for Random functions is aiming to bring a modern, flexible, and developer-friendly way to handle randomness in JavaScript.&lt;/p&gt;

&lt;p&gt;Think of it as Math.random() on steroids — but cleaner, clearer, and more powerful.&lt;/p&gt;

&lt;p&gt;So what’s in the box?&lt;br&gt;
Let’s take a peek at what this proposal brings:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Random.int(min, max)&lt;/strong&gt;&lt;br&gt;
A simple, readable way to get an inclusive random integer between two values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// could be 1, 2, 3, 4, or 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No more manual flooring and adjusting!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Random.float(min, max)&lt;/strong&gt;&lt;br&gt;
Get a floating-point number between min and max:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// behaves like Math.random(), but scoped!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Random.boolean()&lt;/strong&gt;&lt;br&gt;
Flip a coin — easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// true or false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Random.pick(array)&lt;/strong&gt;&lt;br&gt;
Pick a random item from an array — finally built-in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pick&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Random.shuffle(array)&lt;/strong&gt;&lt;br&gt;
Need a shuffled version of an array? It's just one call away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shuffle&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="c1"&gt;// might return [3, 1, 5, 2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pluggable RNGs (even secure ones!)&lt;/strong&gt;&lt;br&gt;
Want cryptographic randomness? Or reproducible random numbers (useful in tests or games)?&lt;br&gt;
The new API supports custom generators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rng&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withSeed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12345&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;rng&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// same output every time for same seed!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or plug in a secure RNG source for critical applications.&lt;/p&gt;




&lt;p&gt;Why not just extend Math.random()?&lt;/p&gt;

&lt;p&gt;The new Random object is separate by design. It allows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple independent RNGs with different behaviors (e.g., seeded vs. secure)&lt;/li&gt;
&lt;li&gt;Cleaner APIs without polluting the global Math object&lt;/li&gt;
&lt;li&gt;Better support for deterministic or cryptographic scenarios&lt;/li&gt;
&lt;li&gt;It’s a modern solution for modern needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Current status?&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;You can track progress and contribute feedback here:&lt;br&gt;
&lt;a href="https://github.com/tc39/proposal-random-functions" rel="noopener noreferrer"&gt;https://github.com/tc39/proposal-random-functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If you’ve ever written &lt;code&gt;Math.floor(Math.random() * (max - min + 1)) + min&lt;/code&gt;, you’ll immediately see the value here.&lt;/p&gt;

&lt;p&gt;And honestly?&lt;br&gt;
&lt;strong&gt;It’s about fu*king time.&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
