Open In App

D3.js shuffler() Method

Last Updated : 23 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of d3.shuffler() method, we can get the random function of shuffle which can be used to shuffle the random values of an array by using this method.

Syntax:

d3.shuffler( random )

Return value: It returns the random shuffle function.

Note: To execute the below examples you have to install the d3 library by using this command prompt we have to execute the following command.

npm install d3

Example 1: In this example, we can see that by using d3.shuffler() method, we are able to get the random shuffle function which we can use it to shuffle the elements in an array by using this method.

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
const random = d3.randomLcg(0.67019185816);
var gfg = d3.shuffler(random);
  
console.log(gfg([0, 1, 2, 3, 4]));


Output:

[ 3, 0, 4, 2, 1 ]

Example 2:

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
const random = d3.randomLcg(0.5816);
var gfg = d3.shuffler(random);
  
console.log(gfg([5, 6, -1, 3, 4]));


Output:

[3, 5, 6, -1, 4]

Example 3:

Javascript




// Defining d3 contrib variable  
var d3 = require('d3');
  
for(var i = 0.1; i <= 0.5; i+=0.1) {
    var random = d3.randomLcg(i);
    var gfg = d3.shuffler(random);
      
    console.log(gfg([-5, 6, -1, 3, -4]));
}


Output:

[ -4, 6, -5, -1, 3 ]
[ -1, 3, -5, -4, 6 ]
[ -5, -4, 6, -1, 3 ]
[ -1, 3, -4, -5, 6 ]
[ 6, -1, -5, -4, 3 ]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads