Friday, May 22, 2015

JavaScript D3 - random integer number

Decimal values are fine, but if you ever need whole numbers, you can use JavaScript’s Math.round() method. For example, you could wrap the random number generator from this line
    var newNumber = Math.random() * 30;
as follows:
    var newNumber = Math.round(Math.random() * 30);
Example:
d3.selectAll("p").style("color", function() { return "hsl(" + Math.round(Math.random() * 360) + ",100%,50%)"; });

No comments: