Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to randomize an array

const getShuffledArr = arr => {
    const newArr = arr.slice()
    for (let i = newArr.length - 1; i > 0; i--) {
        const rand = Math.floor(Math.random() * (i + 1));
        [newArr[i], newArr[rand]] = [newArr[rand], newArr[i]];
    }
    return newArr
};
Comment

randomize an array in javascript

function shuffle(array) {
  let currentIndex = array.length,  randomIndex;

  // While there remain elements to shuffle.
  while (currentIndex != 0) {

    // Pick a remaining element.
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    // And swap it with the current element.
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }

  return array;
}

// Used like so
var arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);
 Run code snippetHide results
Comment

randomize an array

let unshuffled = ['hello', 'a', 't', 'q', 1, 2, 3, {cats: true}]

let shuffled = unshuffled
  .map((a) => ({sort: Math.random(), value: a}))
  .sort((a, b) => a.sort - b.sort)
  .map((a) => a.value)
Comment

Randomise Array

public static T[] SuffeledArray<T>(T[] array, int seed)
{
    var rand = new Random(seed);
  // if you are getting error try
  // var rand = new System.Random(seed);
    for (int i = 0; i < array.Length; i++)
    {
        var randIndex = rand.Next(i, array.Length);
        var tempItem = array[randIndex];
        array[randIndex] = array[i];
        array[i] = tempItem;
    }
    return array;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to find id in array javascript 
Javascript :: capitalize a string javascript 
Javascript :: javascript dynamic arrays 
Javascript :: javascript select change selected 
Javascript :: jquery change position animate 
Javascript :: how to check if checkbox is checked in jquery 
Javascript :: tinymce update textarea value using jquery 
Javascript :: json.stringify vs json.parse 
Javascript :: radio button group get value javascript 
Javascript :: how to access curl data in javascript 
Javascript :: how to make back button react 
Javascript :: javascript is array empty 
Javascript :: nodejs delete s3 folder 
Javascript :: array js fill 
Javascript :: how to make a div appear onclick 
Javascript :: how to get file type in javascript 
Javascript :: setting up fontawesome with react project 
Javascript :: javascript regex reference 
Javascript :: Passing components as children in react 
Javascript :: how to run javascript in chrome 
Javascript :: stream recording javascript 
Javascript :: Lazy Loading Routes vue 
Javascript :: javascript iterate over map keys 
Javascript :: hide a div in jquery 
Javascript :: how make date object in jquery from custom date 
Javascript :: javascript list has item 
Javascript :: Converting string to json object 
Javascript :: javascript Sum of a sequence 
Javascript :: node js event emitter 
Javascript :: how to remove the top border from table react bootstrap 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =