[javascript] How to randomize (shuffle) a JavaScript array?

Community says arr.sort((a, b) => 0.5 - Math.random()) isn't 100% random!
yes! I tested and recommend do not use this method!

let arr = [1, 2, 3, 4, 5, 6]
arr.sort((a, b) => 0.5 - Math.random());

But I am not sure. So I Write some code to test !...You can also Try ! If you are interested enough!

_x000D_
_x000D_
let data_base = []; _x000D_
for (let i = 1; i <= 100; i++) { // push 100 time new rendom arr to data_base!_x000D_
  data_base.push(_x000D_
    [1, 2, 3, 4, 5, 6].sort((a, b) => {_x000D_
      return  Math.random() - 0.5;     // used community banned method!  :-)      _x000D_
    })_x000D_
  );_x000D_
} // console.log(data_base);  // if you want to see data!_x000D_
let analysis = {};_x000D_
for (let i = 1; i <= 6; i++) {_x000D_
  analysis[i] = Array(6).fill(0);_x000D_
}_x000D_
for (let num = 0; num < 6; num++) {_x000D_
  for (let i = 1; i <= 100; i++) {_x000D_
    let plus = data_base[i - 1][num];_x000D_
    analysis[`${num + 1}`][plus-1]++;_x000D_
  }_x000D_
}_x000D_
console.log(analysis); // analysed result 
_x000D_
_x000D_
_x000D_

In 100 different random arrays. (my analysed result)

{ player> 1   2   3  4   5   6
   '1': [ 36, 12, 17, 16, 9, 10 ],
   '2': [ 15, 36, 12, 18, 7, 12 ],
   '3': [ 11, 8, 22, 19, 17, 23 ],
   '4': [ 9, 14, 19, 18, 22, 18 ],
   '5': [ 12, 19, 15, 18, 23, 13 ],
   '6': [ 17, 11, 15, 11, 22, 24 ]
}  
// player 1 got > 1(36 times),2(15 times),...,6(17 times)
// ... 
// ...
// player 6 got > 1(10 times),2(12 times),...,6(24 times)

As you can see It is not that much random ! soo... do not use this method!


If you test multiple times.You would see that player 1 got (number 1) so many times!
and player 6 got (number 6) most of the times!

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to random

How can I get a random number in Kotlin? scikit-learn random state in splitting dataset Random number between 0 and 1 in python In python, what is the difference between random.uniform() and random.random()? Generate random colors (RGB) Random state (Pseudo-random number) in Scikit learn How does one generate a random number in Apple's Swift language? How to generate a random string of a fixed length in Go? Generate 'n' unique random numbers within a range What does random.sample() method in python do?

Examples related to shuffle

Shuffle DataFrame rows What is the purpose of shuffling and sorting phase in the reducer in Map Reduce Programming? Better way to shuffle two numpy arrays in unison How to randomize (shuffle) a JavaScript array? How can I shuffle the lines of a text file on the Unix command line or in a shell script? Random shuffling of an array Shuffling a list of objects Shuffle an array with python, randomize array item order with python