Thread: random sequence without duplicates

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Question random sequence without duplicates

    Hi everyone!

    I am trying to generate a sequence of random numbers between 1-100 without duplicates.

    The problem is that I do not manage to prevent duplicates from being produced.

    The program will serve to avoid the positions in the tables in a championship of poker.

    Thank you in advance.

    That's the code:

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    
      
    int main () {
    
         int nummesas;
         int numjugadores;
         int jugxmesa=10;
         int nummesa;
         int numjug;
         int numpos;
         
         int matriz[10][10];
         
         srand (time (NULL)) ;
         
         printf(" \n");
         printf("Introduce el numero de jugadores: ");
         scanf("%d", &numjugadores);
         printf(" \n");
         if (numjugadores>=0){
                              printf("   - Participan %d jugadores \n \n", numjugadores);
                              nummesas=numjugadores/jugxmesa;
                              if(numjugadores!=100){
                                                    if(numjugadores<=99 && numjugadores>=91) 
                                                                                              nummesas=10;
                                                    if(numjugadores<=90 && numjugadores>=81) 
                                                                                              nummesas=9;
                                                    if(numjugadores<=80 && numjugadores>=71) 
                                                                                              nummesas=8;
                                                    if(numjugadores<=70 && numjugadores>=61) 
                                                                                              nummesas=7;
                                                    if(numjugadores<=60 && numjugadores>=51) 
                                                                                              nummesas=6;
                                                    if(numjugadores<=50 && numjugadores>=41) 
                                                                                              nummesas=5;
                                                    if(numjugadores<=40 && numjugadores>=31) 
                                                                                              nummesas=4;
                                                    if(numjugadores<=30 && numjugadores>=21) 
                                                                                              nummesas=3;
                                                    if(numjugadores<=20 && numjugadores>=11) 
                                                                                              nummesas=2;
                                                    if(numjugadores<=10 && numjugadores>=1) 
                                                                                              nummesas=1;
                                                    }
                              printf("   - Los jugadores ocuparan %d mesas \n \n", nummesas);
                              }
         else 
         printf("El numero de jugadores %d no es valido. \n \n", numjugadores);
         
         printf(" \n\n\n\n");
         system("PAUSE");
         
         system("cls");
    
         for(nummesa=1;nummesa<=nummesas;nummesa++){
                                                    for(numpos=1;numpos<=10;numpos++){
                                                                                      printf("\n\n");
                                                                                      printf("TABLE NUMBER: %d \n\n", nummesa);
                                                                                      printf("POSITION ON TABLE: %d \n\n", numpos);
                                                                                      //aqui una matriz que indique la posición en la mesa
                                                                                      //falta crear la funcion
                                                                                      int c;
                                                                                      int f;                                                                                                   
                                                                                      numjug=(1 + rand() % 100);
                                                                                      printf("\n\n");
                                                                                      printf(" ...................................\n");
                                                                                      printf("\n");
                                                                                      for(c=1;c<6;c++)
                                                                                      printf("\n");
                                                                                      printf(" ...................................\n");
                                                                                      printf("\n\n"); 
                                                                                      printf("Jugador Numero: %d \n\n", numjug);
                                                                                      //comparar con numero registrado en lista
                                                                                      //mostrar jugador asignado a numero
                                                                                      printf("\n\n");
                                                                                      system("PAUSE");
                                                                                      system ("cls");
                                                                                      }
                                                    }
    system("PAUSE");
    return 0;
    }

    The problem is there:

    Code:
    for(nummesa=1;nummesa<=nummesas;nummesa++){
                                                    for(numpos=1;numpos<=10;numpos++){
                                                                                      printf("\n\n");
                                                                                      printf("TABLE NUMBER: %d \n\n", nummesa);
                                                                                      printf("POSITION ON TABLE: %d \n\n", numpos);
                                                                                      //aqui una matriz que indique la posición en la mesa
                                                                                      //falta crear la funcion
                                                                                      int c;
                                                                                      int f;                                                                                                   
                                                                                      numjug=(1 + rand() % 100);
                                                                                      printf("\n\n");
                                                                                      printf(" ...................................\n");
                                                                                      printf("\n");
                                                                                      for(c=1;c<6;c++)
                                                                                      printf("\n");
                                                                                      printf(" ...................................\n");
                                                                                      printf("\n\n"); 
                                                                                      printf("Jugador Numero: %d \n\n", numjug);
                                                                                      //comparar con numero registrado en lista
                                                                                      //mostrar jugador asignado a numero
                                                                                      printf("\n\n");
                                                                                      system("PAUSE");
                                                                                      system ("cls");
                                                                                      }
                                                    }
    sys
    Last edited by carlosmarin7; 08-09-2010 at 07:01 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    ....
    Last edited by carlosmarin7; 08-09-2010 at 07:00 PM.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Wow... At least you indent...

    The only easiest way to do this is to keep a running list of all "random" numbers used in your number generator. So, just some data structure to keep track(I suggest a bmp) and if a number has been used, you could call the number generator recursively, until you find a number that hasn't been used. Be careful to put so check to recognize when all #s have been used, otherwise you'll get an infinite recursive call(That is, until you overflow the stack...).
    Last edited by User Name:; 08-09-2010 at 11:37 PM. Reason: Correction, it's not the only way, but the easiest.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Talking

    Quote Originally Posted by User Name: View Post
    Wow... At least you indent...

    The only way to do this is to keep a running list of all "random" numbers used in your number generator. So, just some data structure to keep track(I suggest a bmp) and if a number has been used, you could call the number generator recursively, until you find a number that hasn't been used. Be careful to put so check to recognize when all #s have been used, otherwise you'll get an infinite recursive call(That is, until you overflow the stack...).
    Thanks!!! Very Grateful!

    Already it is solved!

    That's the code:

    Code:
    void sorteoposiciones (){
         
         int nummesas;
         int numjugadores;
         int jugxmesa=10;
         int nummesa;
         int numjug;
         int numpos;
         
         int matriz[10][10];
         
         int array[101] = { NULL };
         int i, num;
    
         srand((unsigned)time(NULL));
    
         for(nummesa=1;nummesa<=nummesas;nummesa++){
         
         for(numpos=1;numpos<=10;numpos++){
                                           
              do
                {
                 numjug = (rand() % 100) + 1;
                 }
              while(array[numjug]);
    
              array[numjug] = 1;
                                                                                            
              printf("\n\n");
              printf("                    I I   C A M P E O N A T O   P O K E R \n \n");
              printf("                      C I U D A D   D E   S E G O R B E \n \n \n");
              printf("                           1 4   d e   A g o s t o \n \n \n");
              printf("           C O M I S I O N   D E   T O R O S   S E G O R B E   2 0 1 0 \n\n");
              printf(" ..............................................................................\n");
              printf(" \n \n");
                                                                                            
              printf("MESA NUMERO: %d \n\n", nummesa);
                                                                                            
              printf("POSICION EN LA MESA: %d \n\n", numpos);
                                                                                            
              printf("\n\n");
                                                                                            
              printf(" ...................................\n");
                                                                                            
              printf("\n");
                                                                                            
              printf("  - Jugador Numero: %d \n", numjug);
                                                                                            
              matriz[nummesa][numpos]==numjug;
                                                                                            
              printf("\n\n");
                                                                                            
              printf(" ...................................\n");
                                                                                            
              printf("\n");
                                                                                      
              //comparar con numero registrado en lista
              
              //mostrar jugador asignado a numero
              
              //aqui una matriz que indique la posición en la mesa
    
              //falta crear la funcion                                                                       
                                                                                            
              printf("\n\n");
              
              system("PAUSE");
              
              system ("cls");
              
              }
                                                                                      
       }
         }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by User Name:
    The only way to do this is to keep a running list of all "random" numbers used in your number generator. So, just some data structure to keep track(I suggest a bmp) and if a number has been used, you could call the number generator recursively, until you find a number that hasn't been used. Be careful to put so check to recognize when all #s have been used, otherwise you'll get an infinite recursive call(That is, until you overflow the stack...).
    If the number of random numbers is large compared to the range, then you should consider populating an array with the entire range of numbers, then shuffling them and pick out the first n. (You could partially shuffle, if necessary.) In this case, it appears that the number of random numbers required is relatively small, so User Name:'s suggestion is better.

    Quote Originally Posted by carlosmarin7
    Already it is solved!

    That's the code:
    Good to hear that. You might want to check though, e.g., nummesas was never assigned a value, although you did assign it a value in your earlier code. Furthermore, the indentation could be more consistent, and you do not need to indent by what looks like 20+ spaces: 2 to 8 spaces, or a single literal tab, is enough
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Replies: 3
    Last Post: 01-14-2002, 05:09 PM

Tags for this Thread