-1

我正在尝试使用此代码生成随机数并将数字列表保存在数组中,然后我尝试从列表中逐一删除出现一次的数字,例如 1、5、9、4、3、7 ,6 ,10, 11, 8, 2 是整数列表,现在 9 出现一次,现在我不再需要 9 了。这是我的随机非重复数字数组的代码。

NSMutableArray *storeArray = [[NSMutableArray alloc] init];    
BOOL record = NO;
int x;

for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
{
    x = arc4random() % 10;//generating random number
    if(i==0)//for first time 
    {
      [storeArray addObject:[NSNumber numberWithInt:x]];  
    }
    else
    {
        for (int j=0; j<= [storeArray count]-1; j++) 
        {
                if (x ==[[storeArray objectAtIndex:j] intValue]) 
                record = YES;
        }

        if (record == YES) 
        {
            record = NO;
        }
        else
        {
            [storeArray addObject:[NSNumber numberWithInt:x]];
        }
    }
}
4

5 回答 5

2

试试看

//**************remove repeat objects from array***************************//
    NSArray *noDuplicates = [[NSSet setWithArray: yourArray] allObjects];
于 2013-08-07T06:29:41.190 回答
2

你添加
.h 文件

BOOL isSame;
NSMutableArray *countArray;
NSInteger randomNumber;

.m 文件

countArray=[[NSMutableArray alloc]init];

//get randon no
-(NSInteger)getRandomNo:(NSInteger)range
{
    isSame=TRUE;
    while (isSame){
    isSame = FALSE;
    randomNumber = arc4random() % range;
    for (NSNumber *number in countArray){
        if([number intValue] ==randomNumber){
            isSame = TRUE;
            break;
        }
    }
    }
    [countArray addObject:[NSNumber numberWithInt:randomNumber]];
    return randomNumber;
}
于 2013-08-07T06:43:58.063 回答
2

尝试这个,

NSArray *arrRandoms = [[NSArray alloc]initWithObjects:1,5,8,7,36,17,96,32,5,7,8,13,36,nil] ;     // This contains your random numbers

NSMutableArray *arrFresh = [[NSMutableArray alloc]init];

// Now removing the duplicate numbers

BOOL checkRepeat = NO;
int _Current;

for (int i=0; i<[arrRandoms count]; i++)
{
_Current = [arrRandoms objectAtIndex:i];

  if (i == 0)
     [arrFresh addObjects:_Current];
  else
  {
    checkRepeat = NO;

    for(int j=0; j< [arrFresh count]; j++)
    {
     if ( _Current == [arrFresh objectAtIndex:j])
       checkRepeat = YES;
    }

   if (checkRepeat == NO)
     [arrFresh addObjects:_Current];

  }
}

我认为这段代码会起作用。核实。

于 2013-08-07T07:30:40.320 回答
0

已测试请试试这个,

NSMutableArray *storeArray = [[NSMutableArray alloc] init];
NSMutableSet * setUnique = [[NSMutableSet alloc] init];
for (int i=0; [setUnique count] < 10; i++) //Loop for generate different random values
{
    [setUnique addObject:[NSNumber numberWithInt:arc4random() % 10]];

}
storeArray = [[setUnique allObjects] mutableCopy];
于 2013-08-07T07:01:09.290 回答
-1

// 检查如何在数组中获取随机数(即这个数组(低于 arr_numbers)包含不重复的数字)

//出于一般目的,我发布了这个..所以人们无法检查另一个答案

int num_count = 10;
    int RandomNumber;
    NSMutableArray *arr_numbers = [[NSMutableArray alloc] init];

    for (int j =0; j < num_count; j++)
    {
        RandomNumber = 0 + arc4random() % num_count;

        NSLog(@"%d",RandomNumber);

        if ([arr_numbers count]>0)
        {
            if (![arr_numbers containsObject:[NSNumber numberWithInt:RandomNumber]])//
            {
                [arr_numbers addObject:[NSNumber numberWithInt:RandomNumber]];
            }

            if (j == num_count-1)
            {
                if ([arr_numbers count] != num_count)
                {
                    j = 0;
                }
            }
        }
        else
        {
            [arr_numbers addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    NSLog(@"%@",arr_numbers);
于 2013-08-07T07:34:09.520 回答