I do a little fun computing. My mini-project was a simulation of the Italian game "tomboli". A key building block was the simulation of the following process:
The game is controlled by a person with a bag of 90 marbles with numbers from 1 to 90. He draws balls one after another randomly from the bag, each time calling the marble number to the players.
After a little thought, I wrote the following code for this building block:
void bag( int random_sequence[NBR] )
{
int i;
int *store = random_sequence;
int not_yet_pulled[NBR];
for( i=0; i<NBR; i++ )
not_yet_pulled[i] = i+1;
for( i=NBR; i>=1; i-- )
{
int x = rand();
int idx = x%i;
*store++ = not_yet_pulled[idx];
not_yet_pulled[idx] = not_yet_pulled[i-1];
}
}
I know that while modeling games with random numbers there are subtleties and traps, therefore, although I am very pleased with my code, my confidence is slightly less than 100%. So my questions are:
1) Is there something wrong with my code?
2) [if the answer is 1) no] Am I unconsciously using the standard shuffling algorithm?
3) [ 2) ] ?
, . , , -, . , , , . , , . , , , . , , , , , .
, , , , rand()% N 0..N- 1.
, Fisher-Yates , . , .