poker 洗牌算法

扑克游戏中一种洗牌算法的实现:
int count = 54;
NSMutableArray pokeArray = [NSMutableArray alloc] initWithCapacity:count];
// 将牌按顺序摆放
for(int i=0; i [pokeArray addObject:[NSNumber numberWithInt:i]];
}
// 循环将倒数第n张牌中的随机一张放到整个扑克的最后
for(int n=count-1;n>=1;n--){
int rand = (int)(rand()/(float)RAND_MAX*n);
NSNumber *pokeMoveToEnd = [pokeArray objectAtIndex:rand];
[pokeArray removeObjectAtIndex:rand];
[pokeArray addObject:pokeMoveToEnd];
}

你可能感兴趣的:(poker 洗牌算法)