简体   繁体   English

从数组中随机抽取所有元素?

[英]Take all elements from array in a random way?

Hi everyone 嗨,大家好
Can't figure out how can I take element from array in a random way and without repeating. 无法弄清楚如何以随机方式从数组中获取元素,而无需重复。 Here is my code: 这是我的代码:

var letters = [ "A", "A", "B", "B", "C", "C", "D", "D", "E", "E",
           "F", "F", "G", "G", "H", "H", "I", "I", "J", "J" ],
cards = document.getElementsByClassName( "cards" ),
cardBoxes = document.getElementsByClassName( "card-boxes" );


//generate random number
function randomNum( nums ) {
 return Math.floor( Math.random() * nums.length );
}


//hide letter behind card in random way
function hideLetter() {
 for ( var i = cards.length - 1; i >= 0; i-- ) { 
   var randomLetter = letters[ randomNum(letters) ];
   cards[i].textContent = randomLetter;
 };
}
hideLetter();

I take element in a random way, but Math.random repeating themselves. 我以随机的方式处理元素,但是Math.random重复了一次。 I think I have to write some sort of if statment, which will be detect if element was taken two times, but cant figure how to do it. 我想我必须写某种if语句,它将检测是否两次使用了element,但是无法弄清楚该怎么做。 Looking for advice. 寻求建议。 Thanks. 谢谢。

Here Codepen of problem http://codepen.io/Kuzyo/pen/vdlai 问题的Codepen在这里http://codepen.io/Kuzyo/pen/vdlai

The sure way is to remove element from the array after it was used. 确定的方法是在使用元素后从数组中删除它。 This way it will never be repeated 这样就永远不会重复

randomise your array, then walk through the randomised array. 随机化数组,然后遍历随机数组。 This has the benefit of the usual "remove elements while using random indices", while keeping the sequence around in case you need a repeat operation that relies on the same random sorting: 这具有通常的“在使用随机索引时删除元素”的好处,同时保持顺序不变,以防您需要依赖相同随机排序的重复操作:

function randomize(array) {
  var copy = array.slice(),
      random = [],
      element, pos;
  while(copy.length>0) {
    pos = (Math.random()*copy.length) | 0; // bit operation forces 32-bit int
    subarray = copy.splice(pos, 1);
    random.push(subarray[0]);
  }
  return random;
}

var letters = [ "A", "A", "B", "B", "C", "C", "D", "D", "E", "E",
       "F", "F", "G", "G", "H", "H", "I", "I", "J", "J" ],
    randomLetters = randomize(letters);

randomLetters.forEach(function(letter) {
  // create a card for this letter
});

// do we need it again, in a new random fashion?
// then we can simply call this:
randomLetters = randomize(letters);

Here is my version, with a generator. 这是我的版本,带有发电机。 The function returned by makeRandomGenerator will return random, non-repeating members of inputArray . 通过返回的功能makeRandomGenerator将返回随机的,不重复的成员inputArray Once all elements have been used, it will return undefined . 使用完所有元素后,它将返回undefined

function shuffle(array) {
  return array.sort(function() {
    return Math.random() > 0.5 ? 1 : -1;
  });
}

function makeRandomGenerator(inputArr) {
  var arr = inputArr.slice(0);
  shuffle(arr);

  return function() {
    return arr.pop();
  }
}

to use: 使用:

var letterGenerator = makeRandomGenerator(letters);

function hideLetter() {
 for ( var i = cards.length - 1; i >= 0; i-- ) { 
   var randomLetter = letterGenerator();
   if (randomLetter === undefined) {
     return;
   }
   cards[i].textContent = randomLetter;
 };
}
hideLetter();

Randomize the array and shift off values from the array : 随机化数组并移出数组中的值:

var letters   = [ "A", "A", "B", "B", "C", "C", "D", "D", "E", "E", "F", "F", "G", "G", "H", "H", "I", "I", "J", "J" ],
    cards     = document.getElementsByClassName( "cards" ),
    i         = letters.length, j, temp;

while ( --i ) {
    j = Math.floor( Math.random() * (i - 1) );
    temp = letters[i];
    letters[i] = letters[j];
    letters[j] = temp;
}

for ( var i = cards.length; i--; ) { 
     cards[i].innerHTML = letters.shift();
}

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM