简体   繁体   English

如何防止任意两个元素在随机抽取中相同?

[英]How to prevent any two elements from being the same in a random draw?

Here is my code: (Thank you MurifoX) 这是我的代码:(谢谢MurifoX)

<% random_bullets = Bullet.all %>
<ul>
  <% 4.times do %>
    <li><%= random_bullets.sample.content %></li>
  <% end %>
</ul>

I would like to know if making a condition is possible to prevent any two "bullets" from having the same content on the page. 我想知道是否有条件可以防止任何两个“项目符号”在页面上具有相同的内容。 If so, could I get some help on the issue, maybe a nudge in the right direction? 如果是这样,我是否可以在这个问题上获得帮助,也许是朝着正确的方向推进?

Just remove them from the pool as they are used: 只需将它们从池中删除即可:

<% 4.times do %>
  <li><%= random_bullets.delete_at(rand random_bullets.size).content %></li>
<% end %>

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

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