2

I have an array that is rendering 25 images/pins every time a button is clicked, and it is only rendering the first 25 in the array. I want it to render 25 pins at a time but would like them to be random every time the button is clicked.

It was suggested I use part of an answer previously set in here, but I cannot figure out how to apply it or make sense of what I have.

This is what I was told to use:

randomIndex = Math.floor(Math.random() * currentIndex);

And replace current index with the array I want plus .length.

var pinIdArray = ["448460075380400214", "554857616577037440", "129619295506364205"];     
var boardIdArray = [];
var showPins = [];
      
function(){
  res.render("suggested", {pins: showPins});
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126

3 Answers3

1

You could use the Durstenfeld shuffle to shuffle the array and then take the first 25 elements in the array.

function shuffleArray(array) {
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  return array;
}

var array = [];
for(var i = 0; i < 500; i ++) {
    array[i] = i;
}
console.log(shuffleArray(array).slice(0, 25));
Community
  • 1
  • 1
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
1

Here is a function implemented in Javascript without any libraries. This also avoids shuffling the array so should be faster.

let pinIdArray = ["448460075380400214", "554857616577037440", "129619295506364205", "3403722138", "8005795986", "7717201977", "6689430878", "7705363504", "3827905985", "9133621064", "9162201846", "2655432017", "0197893312", "7220269979", "3218703261", "3478813716", "7445481990", "9806757977", "9357022147", "3492330721", "3504169963", "9259212333", "6574699545", "9727263383", "0016479256", "1624997250", "2083975447", "5683391989", "3466001901", "4660933902", "5216511997", "8820216343", "8583764035", "4563326839", "5201961267", "3429608185", "5007846054", "7437408815", "3472117054", "1545827364", "3152159572", "7913372317", "2550237417"];

function getRandomSubset(array, setSize) {
  let maxValue = array.length, tmpSet = new Set(), randomIndices;
  if (maxValue <= setSize) {
    randomIndices = [...Array(setSize).keys()];
  } else {
    while (tmpSet.size < setSize) {
      tmpSet.add(Math.floor(Math.random() * maxValue));
    }
    randomIndices = Array.from(tmpSet)
  }
  return randomIndices.map(index => array[index]);
}

//function(){
//  res.render("suggested", {pins: getRandomSubset(pinIdArray, 25)});

// Test
console.log("25 Random Elements:", getRandomSubset(pinIdArray, 25));
Deekshith
  • 1,544
  • 2
  • 14
  • 15
0

You can randomize/shuffle the values in an array by using Lodash's _.shuffle function. The Lodash _.shuffle function uses a version of the Fisher-Yates shuffle.

var array = [1, 2, 3, 4];
var result = _.shuffle(array)
console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
A_A
  • 310
  • 4
  • 13