从给定数字生成随机数 [英] Generate random number from given number

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

问题描述

我陷入一个有关随机数的问题。我正在使用 arc4random()生成随机数。我想首先生成0-9之间的随机数。所以我正在使用:

I am stuck in one issue regarding to random number. I am generating random number using arc4random(). I want to generate randome numbers between 0-9 at first. so i am using:

int RandomNumber = arc4random() % 9;

很好。现在假设我有8。我不希望我的新随机数是8。所以我做了以下工作:

its good. Now suppose I got 8. I don't want my new random number to be 8. So I have done following:

NSMutableArray *aryTemp = [NSMutableArray new];
for (int i = 0; i < [aryTemp Count] < 9; i++) {
   int RandomNumber;
   do {
      RandomNumber = arc4random() % 9;
   }
   while ([aryTemp containsObject:[NSNumber numberWithInt:RandomNumber]])
   [aryTemp addObject:[NSNumber numberWithInt:RandomNumber]];
}

所以我生成一个随机数,而不是将其添加到数组中,而不是检查下一个随机数是上一个生成的随机数吗?

So I am generating a random number than adding it to array than checking for the next random number that is this the previous generated random number or not? If not than add it to the array.

对于一些起始数字来说,它工作得很好,但是当我离开时很少选择时,产生欲望的机会就很少了。随机数,而while循环过程由于冗余的随机数而花费大量时间。

It works fine for few starting numbers but when I left of with few option at that time I have very less chances to generate desire random number and while loop processes takes lots of time due to redundant random number.

我要寻找的是我想要的随机数0、1、2、3、4、5、6、7、8、9
如果我下次第一次获得6时,随机数中只能包含0、1、2、3、4、5、7、8、9

What I am looking for I want random numbers of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 If I got 6 at first time next time the random number must be with in these numbers only 0, 1, 2, 3, 4, 5, 7, 8, 9

推荐答案

编辑:上面评论中的答案提出了一种更为优雅的解决方案-改组数组,尽管它使用的是与我朴素的方法相同的原理:)

The answer linked in the comments above proposes a more elegant solution - shuffling the array in place, although it uses the same principal as my naive approach :)

如果我正确理解:您想从0-9范围内选择所有项目

If I understand you correctly: You are wanting to select all items from the range 0-9 at random.

我最初的方法是创建一个包含所有范围内物品的袋子。每次您随机选择一个项目-将其从包中取出,以免再次选择。

My initial approach would be to create a bag with all the items in the range. Each time you randomly select an item - remove it from the bag so you don't select it again.

以下代码段应实现以下目的:

The following code snippet should achieve this:

int randomNumber;

// Fill remainingNumbers array with numbers 0-9
NSMutableArray *remainingNumbers = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
    [remainingNumbers addObject:[NSNumber numberWithInt:i]];
}

// Create tempNumbers array to store our random numbers in
NSMutableArray *tempNumbers = [NSMutableArray array];


for (int i = 0; i < 9; i++) {
    // Create a random number in the range of the indexs for remainingNumbers array
    int maxIndex = [remainingNumbers count] -1;
    randomNumber = arc4random() % maxIndex;

    // Add random number to tempNumbers array
    [tempNumbers addObject:[remainingNumbers objectAtIndex:randomNumber]];

    // Remove the number from the remaining numbers so we don't add duplicates
    [remainingNumbers removeObjectAtIndex:randomNumber];
}
// Add final number
[tempNumbers addObject:[remainingNumbers objectAtIndex:0]];


// Print the array of 'randomly selected numbers'
NSLog(@"The array is: %@", tempNumbers);

这篇关于从给定数字生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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