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.
<!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" </ 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
<!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" </ 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.