簡體   English   中英

從數組中獲取兩個隨機的非連續且唯一的元素

[英]Getting two random non consecutive and unique elements from an array

嗨,我正在嘗試從不連續的列表中獲取兩個唯一的隨機元素。 數組形成如下

[
  {"id": 1, "name": "Monday", "workers": []},
  {"id": 2, "name": "Tuesday", "workers": []},
  {"id": 3, "name": "Wednesday", "workers": []},
  {"id": 4, "name": "Thursday", "workers": []},
  {"id": 5, "name": "Friday", "workers": []},
]

我設法獲得了以下兩個獨特的元素:

 getTwoNonConsecutiveDays = () => {
    var days = require('./weeks.json');

    let selected = [];
    let randomday = () => {
      const shuffled = days.sort(() => 0.5 - Math.random());

      // Get sub-array of first n elements after shuffled
      selected = shuffled.slice(0, 2);


但是,當我試圖使條件不連續時不起作用

      if (selected[0].id === selected[1].id) {

     console.log('greseala')}

     selected[0].id

你可以 go,像這樣:

  • 選擇隨機居中的 window 的 3 個后續項目(如果從第一個數組項目開始,則為 2 個)並將中間的一個作為第一個項目
  • 刪除選定的三元組(例如使用Array.prototype.splice() )以避免選擇第一個隨機項目或其鄰居
  • 從剩下的那些中隨機選擇第二個隨機項目

 const src = [{"id":1,"name":"Monday","workers":[]},{"id":2,"name":"Tuesday","workers":[]},{"id":3,"name":"Wednesday","workers":[]},{"id":4,"name":"Thursday","workers":[]},{"id":5,"name":"Friday","workers":[]}], getRandomPair = ([...a]) => { const {random} = Math, windowPos = 0|random()*(a.length-1), e1 = a.splice(windowPos, windowPos? 3: 2)[windowPos && 1], e2 = a[0|random()*a.length] return [e1,e2] } console.log(getRandomPair(src))
 .as-console-wrapper{min-height:100%;}

你可以這樣處理:

var days = require('./weeks.json');
 function randomIndex(array){
        return Math.floor(Math.random() * array.length);
 }
 
 function randomPair(array){
        var index1 = randomIndex(array), index2;
    do{
        index2 = randomIndex(array);
    }while(Math.abs(index1 - index2) < 2);
    return [array[index1], array[index2]];
 }
console.log(randomPair(days));

注意:正如@Yevgen Gorbunkov 在評論中所說,while 循環和退出條件不適用於小型 arrays。 可以添加一個額外的 if 條件來檢查數組的長度。

更新:根據@Andreas 評論更新條件時,使用 Math.abs 而不是多重檢查。

您可以嘗試以下邏輯:

  • 創建一個以n為限制的循環。
  • 創建 2 個變量:
    • List以保存項目。
    • map保存生成的索引。
  • 循環並生成隨機索引。 要將其保持在界限內,請使用Math.floor( Math.random() * array.length )
  • 現在檢查map是否存在相鄰元素。 如果是,您可以 go 到下一個值。
  • 在找到唯一索引時,將其添加到map並將項目推送到list

 function getNonConsecutiveDays(array, n) { const list = []; const map = {}; Array.from({ length: n }).forEach(() => { let index = Math.floor( Math.random() * array.length ); if( map[ index + 1 ] || map[ index - 1]) { index = (index + 1) % array.length; } let item = array[index]; map[ index ] = true; list.push(item) }); return list; } const data = [ {"id": 1, "name": "Monday", "workers": []}, {"id": 2, "name": "Tuesday", "workers": []}, {"id": 3, "name": "Wednesday", "workers": []}, {"id": 4, "name": "Thursday", "workers": []}, {"id": 5, "name": "Friday", "workers": []}, ]; console.log( getNonConsecutiveDays(data, 2) )

這是我想出的:

  1. 從范圍[0, arr.length)中選擇第一個元素的索引a
  2. 如果a == 0a == arr.length - 1 ,則第一個元素只有 1 個鄰居。 否則,它有 2 個。 所以第二個索引的可能選擇數等於arr.length - 1 - neighbours
  3. 從范圍[0, arr.length - 1 - neighbours)中選擇第二個元素的索引b
  4. 如果b >= a - 1 ,將1 + neighbours添加到b

這是代碼:

 arr = ['A', 'B', 'C', 'D'] a = Math.floor(Math.random() * arr.length); // 1. neighbours = 2; // 2. if (a == 0 || a == arr.length - 1) neighbours = 1; b = Math.floor(Math.random() * (arr.length - 1 - neighbours)); // 3. if (b >= a - 1) b += neighbours + 1; // 4. console.log(arr[a], arr[b]);

應該保證你的數組的長度大於 3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM