简体   繁体   中英

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

Here is my code: (Thank you 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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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