从NSMutablearray生成随机数

时间:2013-06-05 06:04:27

标签: iphone ios objective-c

我需要从我在for循环中尝试过的数组生成随机数

rnd = arc4random_uniform(arr.count); 

它生成随机数但有些数字重复,也在math.h中尝试了 Random() 还是同样的问题仍然存在请帮帮我.. 提前谢谢..

7 个答案:

答案 0 :(得分:2)

如果我理解

  

“我需要从数组中生成随机数”

正确地说,您希望随机从数组中获取数字,如果是这样的话  首先将数字存储在NSMutableArray

NSMutableArray *arr=//store your numbers in this array.

-(void)getRandomNumberFromArray:(NSMutableArray *)arr{
int r = arc4random() % arr.count;
int number=[arr objectAtIndex:r];
[arr removeObjectAtIndex:r];
}

答案 1 :(得分:1)

为每个元素生成一个随机数,然后检查以确保它与现有元素不同。如果它已经存在,请再试一次。

for(int i = 0; i < arr.count;i++)
{
   BOOL isRandom = YES;
   int rand = -1;
   while(!isRandom )
   (
     isRandom = YES;
     rand = arc4random() % 5;
     for(int j = 0; j < i; j++)
     {
        int existingNumber = arr[j];
        if(existingNumber == rand)
        {
           isRandom = NO;
           break;
        }
     }
  }
  arr[i] = rand;
}

另一种选择是首先将它们分配给具有递增值,然后将可变数组洗牌。 What's the Best Way to Shuffle an NSMutableArray?

答案 2 :(得分:1)

如果我理解你的问题,那么你不需要随机数,你需要改组数组中的元素吗?

然后你需要使用这个方法,

-(void)shuffleWithArray:(NSArray*)cardsArray
{

    NSMutableArray *shuffleArray =[[NSMutableArray alloc]initWithArray:cardsArray];


   // NSUInteger count1=[shuffleArray count];
   for (NSUInteger i= 0; i<[shuffleArray count]; i++) 
    {

    int nElement=[shuffleArray count] -i;
    int n=(arc4random()%nElement + i);
    [shuffleArray exchangeObjectAtIndex:i withObjectAtIndex:n];

  }

}

答案 3 :(得分:0)

查看以下教程。它必须对你有帮助。

iOS Random Number generator

答案 4 :(得分:0)

#include <stdlib.h>


int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (arr.count);
else
    r = (arc4random() % arr.count);

int randomNumberFromArray=[arr objectAtIndex:r];

修改

宾果。经过一番思考后我才开始工作

-(NSArray *)randomizeArray:(NSArray *)inputArray
{
    NSMutableArray *checkArray=[[NSMutableArray alloc]initWithCapacity:3];
    NSMutableArray *outputArray=[[NSMutableArray alloc]initWithCapacity:3];

    while ([outputArray count]<[inputArray count])
    {
        int r=[self getRandomNumber:[inputArray count]];
        if ([checkArray containsObject:[NSNumber numberWithInt:r]])
        {


        }
        else
        {
            [checkArray addObject:[NSNumber numberWithInt:r]];
            [outputArray addObject:[inputArray objectAtIndex:r]];
        }
    }
    return outputArray;
}


-(int)getRandomNumber:(int)maxValue
{
    int r = 0;
    if (arc4random_uniform != NULL)
        r = arc4random_uniform (maxValue);
    else
        r = (arc4random() % maxValue);
    return r;
}

这个randomizeArray:方法将为您提供随机化的整个数组

答案 5 :(得分:0)

您可以使用以下功能。

@property (nonatomic,strong) NSMutableArray * numbers;


- (NSInteger) nonRepeatedNumber
{

   if(_numbers == nil)
    {
       _numbers = [[NSMutableArray alloc]init];
    } 

   NSInteger number = arc4random()% arr.count;
   while ([_numbers containsObject:[NSNumber numberWithInt:number]])
   {
       number = arc4random()% arr.count;;
   }
  [_numbers addObject:[NSNumber numberWithInt:number]];

   return number;
}

答案 6 :(得分:0)

尝试以下方法

 -(NSMutableArray*)randomNumbersfromArray:(NSArray*)array    {
        NSArray *numbers = array;
        int count = [numbers count];
        NSMutableArray *addedIndexes = [[NSMutableArray alloc]initWithCapacity:count];
        NSMutableArray *addedObjects = [[NSMutableArray alloc]initWithCapacity:count];
        int i = 0;
        while ([addedIndexes count] != [numbers count]) {

            int random = arc4random() % count ;
            if (![addedIndexes containsObject:[NSString stringWithFormat:@"%i",random]]) {

                [addedIndexes addObject:[NSString stringWithFormat:@"%i",random]];
                [addedObjects addObject:[numbers objectAtIndex:random]];
                i++;
            }

        }
        return addedObjects;
    }