Saturday, November 15, 2025
HomeLanguagesJavascriptD3.js randomUniform() Function

D3.js randomUniform() Function

The randomUniform() function in D3.js is used to return a random number in a particular range. The range is to be defined by the max and min number.

Syntax:

d3.randomUniform(min, max);

Parameters: It takes two parameters as mentioned above and described below:

  • min: It is the minimum value of the random number possible.
  • max: It is the maximum value of the random number possible.

Return Value: It returns the random number between the given range.

Note: The output of the code will be different each time it is executed.

Below given are a few examples of the above function.

Example 1: When min and max ranges are given.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
  
<body>
  
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
      
        // Random number between 1 and 10
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
        console.log(d3.randomUniform(1, 10)())
    </script>
</body>
  
</html>


Output:

Example 2: When min and max are equal

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
      
        // Random number less then 10 and
        // greate than 0
        console.log(d3.randomUniform(10)())
          
        // Random number equal to 10
        console.log(d3.randomUniform(10, 10)())
    </script>
</body>
  
</html>


Output: The output may be different every time the code is executed.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6890 POSTS0 COMMENTS
Ted Musemwa
7143 POSTS0 COMMENTS
Thapelo Manthata
6838 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS