0
let colors = ['blue','red','orange','purple','gray','yellow']
let randomColors = [];

I would like push 3 random colors from the the colors array into randomColors. I dont want any of the colors to be repeated.

Seems simple, but ive spent a few hours not being able to make it not repeat itself. Thanks !

born2gamble
  • 197
  • 7
  • 17

4 Answers4

1

The easiest way is to use shuffle function in lodash package. https://lodash.com/docs#shuffle
Then select the the number of colors you want from the randomized array:

const colors = ['blue','red','orange','purple','gray','yellow'];
const selectedColors = _.shuffle(colors).splice(0, 3);  // 3 random colors
console.log(selectedColors);
FisNaN
  • 2,517
  • 2
  • 24
  • 39
  • 1
    Is it the easiest if you don’t have lodash? Surely there is an easy way of doing it vanilla JavaScript..... – Matt Mar 06 '18 at 01:36
  • https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm the link of how to implement Fisher–Yates shuffle in js – FisNaN Mar 06 '18 at 01:39
0

You could use something like this:

    let colors = ['blue','red','orange','purple','gray','yellow'];
    let randomColors = [];

    // log our initial output
    console.log ( "colors: " + colors );
    console.log ( "randomColors: " + randomColors );

    // this is the loop that pulls colors out of the colors array at random and assigns them to the randomColors array
    // this function is destructive on the colors array, so copy it first if you want to keep the array intact
    // The iterations var can be changed to however many you like, but it will fail if you specify a number larger
    // than there are colors in the array
    var iterations = 3;
    for ( var i = 0; i < iterations; i++ ) {
        // grab a random color from the initial array
        var item = colors[Math.floor(Math.random()*colors.length)];
        var idx = colors.indexOf(item);
        // add it to the randomColors array
        randomColors[i] = item;
        // remove it from the colors array so it isn't picked again
        colors.splice(idx, 1);
    }

    // log our resultant output for comparison
    console.log ( "colors: " + colors );
    console.log ( "randomColors: " + randomColors );
0
let excludedIndexes = [];
let currentIndex = null;
let colors = ['blue','red','orange','purple','gray','yellow'];
let randomColors = [];

function getNextRandom(max)
{
    return Math.floor(Math.random()*max);
}

function addColors(numColors)
{
    let i = 0;
    randomColors = [];
    excludedIndexes = [];
    while(i < numColors)
    {
        currentIndex = getNextRandom(colors.length);
        while(excludedIndexes.indexOf(currentIndex)!=-1)
        {
            currentIndex = getNextRandom(colors.length);
        }
        randomColors.push(colors[currentIndex]);
        excludedIndexes.push(currentIndex);
        i++;
    }
}
addColors(3);//modifies the randomColors array each time it's called

Tested in the browser console.

Had to fix a couple lines, but this should work fine now.

webnetweaver
  • 192
  • 8
0

Maybe a clumsy implement

function random_num_between(min,max){
  return Math.floor(Math.random()*(max-min+1)+min);
}

function pick_one(list){
  const index = random_num_between(0,list.length-1)
  return list[index]
}

function get_elements_distinct(originList,num){
  const map = {}
  let counter = 0
  while(counter < num){
    const key = pick_one(originList)
    if(map[key] == null){
      counter ++
      map[key] = 1
    }
  }
  return Object.keys(map)
}

Then you can use it

let colors = ['blue','red','orange','purple','gray','yellow']
let randomColors = get_elements_distinct(colors,3);
kobako
  • 636
  • 5
  • 11