Is it correct to use JavaScript Array.sort() method for shuffling?

前端 未结 12 1743
广开言路
广开言路 2020-11-22 03:48

I was helping somebody out with his JavaScript code and my eyes were caught by a section that looked like that:

function randOrd(){
  return (Math.round(Math         


        
12条回答
  •  执念已碎
    2020-11-22 04:31

    If you're using D3 there is a built-in shuffle function (using Fisher-Yates):

    var days = ['Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche'];
    d3.shuffle(days);
    

    And here is Mike going into details about it:

    http://bost.ocks.org/mike/shuffle/

提交回复
热议问题