How to grab a random element from an array without grabbing the same element more than once?
本问题已经有最佳答案,请猛点这里访问。
当我使用
1 | var definitions = ["Used to carry the pharoah","Used to carry bodies as a ceremony","Had a flat deck to carry a farmer's treasure","Daily, it made a trip around the world to carry Ra","Towed by smaller boats, carrying heavy objects","Used for business and pleasure by officials/nobles","Carried most Egyptians and some goods"] |
在我的
1 | self.boats = [Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))]),Boats(name: definitions[Int(arc4random_uniform(7))])] |
号
如何修改代码,使两个元素不会在我的
保留一个随机索引值的列表,如果随后的随机索引在该列表中,则跳过该列表并生成另一个随机索引。