Hey

The title is a bit confusing so Ill explain.

I have a function that returns random values from 0-4 for the computer (which has numberofplayers). Lets say number of players is 2. One is me and the other is the PC. I (myself thru a scanf and I being player 1) set my value for 2. How can I create some kind of for/do/if/etc that keeps generating numbers until player 2 has a number differerent than player 1 (me)? Obviously the 0-4 range and the number of player changes depending on the options I set.


If someone needs more explaining or some kind of code example, please go ahead and post.

Kinda important so bumping up...

Comments
kinda not actually - http://www.catb.org/~esr/faqs/smart-questions.html#urgent

>The title is a bit confusing so Ill explain.
Unless you didn't say what you meant, I understand your problem perfectly just from the title. Have you tried storing the used random numbers and then searching that list when you need another? A naive solution is pretty obvious and trivial to implement, so I'm leaning toward thinking that you're too lazy to put forth any real effort.

>Kinda important so bumping up...
Bumping is a fantastic way to make sure your question doesn't get answered. It's presumptuous to believe that you're more important than everyone else, and many of us don't want to help people like that.

Have you tried storing the used random numbers and then searching that list when you need another?

Obviously, this is the solution the problem is how to implant it.
Do I make another array storing all the numbers?
And/or how?
lets say we have player[n]
player[1] says 1 (because he typed it in)
player[2] gets a generated number of 3 (because the generator returned him that number)
player[3] gets a generated number of 2 (fine again)
player[4] gets a generated number of 1; No. The generator must regenerate another number because this number has been said by one of the players before him. This time 3. Again no because player[2] said 3. Another generation this time 0. Next player...
Next player[5] gets generated 0. Again. 1. Again. 3. Again. 1. Again. 3. Again 2. Again. 0. Again. 2. Again. 3. Again. 4. FINALLY gets a good number

Thats bascially what I want to do. Im just not sure how to do it. Im not asking for the actual code just ideas on how to do it.

Have an array for each possible number, have each element be a flag as to whether the number at that index has been generated.

So if this were a pack of cards, player1 would choose a card, and the others would be dealt a card from the remaining deck.

Think of it in these terms,
- shuffle the deck
- find card (and remove it)
- deal card (from the top of the deck, and remove it)

So if this were a pack of cards, player1 would choose a card, and the others would be dealt a card from the remaining deck.

Think of it in these terms,
- shuffle the deck
- find card (and remove it)
- deal card (from the top of the deck, and remove it)

Can't be a card because you know there are only 1-12 cards. This number can be infinite.

And yes I already thought of the array but I wanted to check if there was a easier/better way. I guess I'll just do it with another array comparing the array which stores the numbers.

>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;
}

> Can't be a card because you know there are only 1-12 cards
So that's an array of 12, filled with whatever you want, and then shuffled.

This article has been dead for over six months. Start a new discussion instead.