根据一个整齐的数据,随机的得到一个新的数组,可指定数组元素出现的位置

+ (NSArray *)getRandomWithPosition:(NSInteger)position positionContent:(id)positionContent array:(NSArray *)baseArray {
    
    NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:baseArray.count];
    NSMutableArray *tempBaseArray = [NSMutableArray arrayWithArray:baseArray];
    
    while ([tempBaseArray count]) {
        NSInteger range = [tempBaseArray count];
        id string = [tempBaseArray objectAtIndex:arc4random()%range];
        [resultArray addObject:string];
        [tempBaseArray removeObject:string];
    }
    // 计算指定数组元素在新数组中的位置
    NSUInteger index = [resultArray indexOfObject:positionContent];
// 交换数组元素 [resultArray exchangeObjectAtIndex:index withObjectAtIndex:position]; return resultArray; }

  

posted @ 2015-03-24 22:44  菜鸟程序猿  阅读(264)  评论(0编辑  收藏  举报