>This number can be infinite.
Not when you're limited by the data type. If you use int, the limit is 32,767. When a programmer says "infinite", I always cringe because it means he isn't thinking about edge cases.
>And yes I already thought of the array but I wanted
>to check if there was a easier/better way.
There are easier/better ways, but if you're having trouble implementing the obvious solution, you'll have even more trouble with the better ones. All you need to do is generate a new number, then search the stored numbers for a match. If there's a match, generate a new number. Repeat until there's not a match:
#include <stdio.h>
#include <stdlib.h>
int exists ( int key, int list[], int size )
{
int i;
for ( i = 0; i < size; i++ ) {
if ( list[i] == key )
return 1;
}
return 0;
}
int main ( void )
{
int total_players;
printf ( "Number of players: " );
fflush ( stdout );
if ( scanf ( "%d", &total_players ) == 1 ) {
int *players = malloc ( total_players * sizeof *players );
if ( players != NULL ) {
int i;
/* Generate unique random numbers for each player */
for ( i = 0; i < total_players; i++ ) {
do
players[i] = rand() % total_players;
while ( exists ( players[i], players, i ) );
}
/* Display the generated numbers */
for ( i = 0; i < total_players; i++ )
printf ( "Player[%d]: %d\n", i + 1, players[i] );
free ( players );
}
}
return 0;
}