简体   繁体   English

从数组中随机提取的最佳方法?

[英]Best way to randomly pull from an array?

I have a website that will give you a random fact from Wikipedia when you click a big red button. 我有一个网站,当您单击红色的大按钮时,它将为您提供来自Wikipedia的随机事实。 I have heard from a few people that they are getting certain facts repeatedly, even though there are over 200. 我从一些人那里听说,即使有200多个人,他们也在不断地获得某些事实。

The big red button has onclick="giveafact()" which triggers this function: 红色的大按钮具有onclick="giveafact()" ,可触发此功能:

function giveafact(){ //instead of relisting all array items here, add all the other arrays to this one
  var factsList= foodFactsList.concat(musicFactsList,historyFactsList,popFactsList,sportsFactsList,technologyFactsList,televisionFactsList,miscFactsList);
  randomFact = Math.floor(Math.random()*factsList.length);
  document.getElementById("total").innerHTML=factsList.length;
  document.getElementById("fact").innerHTML=factsList[randomFact];
  updateShareLinks();
  return false;
}

Basically, I have 8 different arrays of facts as you can see in var factsList above. 基本上,我有8个不同的事实数组,如您在上面的var factsList This is so the user can filter by fact. 这样用户就可以按事实进行过滤。 By default, there is no filter so all lists are concatenated. 默认情况下,没有过滤器,因此所有列表都是串联的。

If it helps, the full .js file is here: http://thewikifix.com/scripts/script.js . 如果有帮助,则完整的.js文件位于此处: http : //thewikifix.com/scripts/script.js These random "give a fact" functions start at about line 442, with the arrays above them. 这些随机的“提供事实”功能从第442行开始,其上方为数组。 (Pardon the messy code, I know I mix jQuery and Javascript a lot.) (请原谅凌乱的代码,我知道我经常混用jQuery和Javascript。)

The site is http://thewikifix.com , if it helps anyone to look at all the code. 该网站为http://thewikifix.com ,如果它可以帮助任何人查看所有代码。

I'm just trying to see if there's a way to better randomize the facts than I currently have, maybe by adding a function that won't allow a fact to show up twice in a row, or something similar. 我只是想看看是否有一种方法可以比我目前更好地随机化事实,也许是通过添加一个不允许事实连续显示两次的函数或类似的方法。

Any suggestions would be great! 任何建议都很好!

Edit - Additional thoughts: Thanks for the answers so far. 编辑-其他想法:感谢到目前为止的回答。 Is there a way to remove an item from an array once it has been picked via the Math.random function so it just wouldn't show up again at all (unless the page was refreshed)? 一旦通过Math.random函数将其从数组中删除,是否有办法从数组中删除它,使其根本不会再次显示(除非刷新了页面)? If so, once all items were removed from the array is there a way to reset the array to its original state without the user having to refresh? 如果是这样,一旦从阵列中删除了所有项目,是否有办法将阵列重置为其原始状态而无需用户刷新? Thanks. 谢谢。

"Random" and "varied" are, to some extent, conflicting. 在某种程度上,“随机”和“可变”是矛盾的。 With 200 facts, and one selected randomly each time, it becomes more likely than not that you'll see a repeat after getting only a couple dozen or so (this is known as the "Birthday Problem"). 有了200个事实,并且每次都随机选择了一个事实,则很有可能在打了几打左右后就会看到重复(这被称为“生日问题”)。

A simple approach is to store a seed and an index on the client. 一种简单的方法是在客户端上存储种子和索引。 The seed is set once, and the index is incremented after each fact access. 种子设置一次,每次访问事实后索引都会增加。 To get a "random" fact, seed a PRNG with the seed, use the PRNG to shuffle the list of facts, then get the fact at the given index in the shuffled list. 要获得“随机”事实,请在种子中植入PRNG,使用PRNG对事实列表进行混洗,然后在经过重排的列表中的给定索引处获得事实。 When you run out, pick a new seed and reset the index. 用完后,选择一个新种子并重置索引。

Here's what I would do: 这就是我要做的:

n = 0 - Find a random fact
  add that fact to a new array (arr)
  display fact

n + 1 - Find a random fact
  check arr using lodash for that facts existence
  display or find a new fact, based on result of above

--OR-- - 要么 -

you could take the arr and use lodash's _.shuffle() to mix them up and display them in order. 您可以使用arr并使用lodash的_.shuffle()混合它们并按顺序显示它们。

https://lodash.com/docs https://lodash.com/docs

I love using lodash for collection and array operations. 我喜欢将lodash用于收集和数组操作。

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

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