0

I am creating my first card game, and need to shuffle the cards. So for starters, I have created a shuffle button for a sample stack of 4 values with the line:

int i = arc4random() % 4;
NSLog(@"%d", i);

Shuffling works well, but I want this button to allow the user to distribute random cards until there are no more cards in the stack. Each time I click this sample button, I need NSLoged result to be something like 3, then 1, then 0, then 2, then "No more cards", for example (instead of a list of four random numbers and a message).

Is there a simple way to "distribute a number" randomly with each click of a button?

Also, does arc4random generate real random numbers or pseudo-random numbers? I have read a lot of threads about it, and it doesn't seem so clear. What would be the best way to randomize numbers?

4

1 に答える 1

1

ランダム空間で可能なすべての要素を含む配列を作成できます。次に例を示します。

NSMutableArray *randomNumber = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3], nil];

次に、配列の要素をランダムに取得し、次の後にそれらを削除します。

for (int i = 0; i < 4; i++){

int randIndex = arc4random()%[randomNumber count];
NSLog(@" %d %d", randIndex, [[randomNumber objectAtIndex:randIndex] intValue]);
[randomNumber removeObjectAtIndex:randIndex];
}
于 2012-06-20T08:49:35.453 に答える