随机数列表 - arc4random [英] List of random numbers - arc4random

查看:99
本文介绍了随机数列表 - arc4random的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个0-9的数字数组,并希望它们被随机化

I want to create an array of numbers from 0-9 and want them to be randomized

意思是,当用户点击UIButton时,它会创建一个NSMutableArray对象4,5,8,3,6,2,9,1,7,0

Meaning, when a user clicks on a UIButton it creates an NSMutableArray of objects 4,5,8,3,6,2,9,1,7,0

当用户再次点击该按钮时,它会生成另一个0-列表9个随机数字等。

When the user clicks on the button again it generates another list of 0-9 random numbers and so on.

我遇到的问题是arc4random例程。该例程将一次吐出0-9之间的随机数。我必须保存它吐出的数字并将其存储到数组中。然后我将检查它吐出的下一个随机数是否已经在数组中,如果没有,则将其添加到数组中,否则保持循环直到找到不在我的数组中的数字。继续这种疯狂,直到我的数组大小为10.

The problem I have is with arc4random routine. That routine will spit out a random number between 0-9 one at a time. I gotta save that number it spits out and store it into an array. I will then check to see if the next random number it spits out is already in the array or not, if not then add it in the array otherwise keep looping till it finds a number that is not in my array. Keep doing this madness till my array size is 10.

对于0-9的小数组来说,这一切都很好。如果我需要创建一个0到1000之间的随机数组,该怎么办?

Its all well and good for a small array of 0-9. What if I needed to create a random array of lets say between 0 - 1000.

我正在寻找的是一种不需要5年才能完成的有效方法。有什么想法吗?

What I am looking for is an efficient method that wont take 5 years to complete. Any thoughts?

推荐答案

根据这个SO答案什么是最好的洗牌和洗牌的方式,只需创建你的数字列表0..9,(或0..1000,无论如何)在一个可变数组中然后随机地随机播放它们。

As per this SO answer whats-the-best-way-to-shuffle-an-nsmutablearray, just create your list of numbers 0..9, (or 0..1000, whatever) in a mutable array and then randomly shuffle them.

这篇关于随机数列表 - arc4random的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆