How to create a No-repeat random array

This is a discussion on How to create a No-repeat random array within the C Programming forums, part of the General Programming Boards category; Hi, i need to make a program like Master Mind on C, but first i need to create an array ...

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

    Cool How to create a No-repeat random array

    Hi, i need to make a program like Master Mind on C, but first i need to create an array of 4 random values but they can't repeat, is there any insight, or any clue for how can i make this happen. Thank you very much in advance and btw this is my first thread ever on cprogramming.com!

    PD this is the code that i have allready.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    void GenerarCombinacion(char Combinacion[4]);
    
    main ()
    {	
    	char Combinacion[4];
    	GenerarCombinacion(Combinacion);
    }
    
    void GenerarCombinacion(char Combinacion[4])
    {
    	int i=0, Temporal, Temporal2;
    	char Colores[7];
    	Colores[0]='A';
    	Colores[1]='R';
    	Colores[2]='V';
    	Colores[3]='G';
    	Colores[4]='M';
    	Colores[5]='N';
    	Colores[6]='B';
    	srand(time(NULL));
    	Temporal=(rand()%100);
    	for(i=0;i<4;i++)
        {	
    		Temporal=(rand()%7);
    		Combinacion[i]=Colores[Temporal];
    		Temporal=(rand()%7);
    		Combinacion[i]=Colores[Temporal];
    	}
    			puts(Combinacion);
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    If you have only 4 numbers, just pick a random one and make sure it is not the same as any you have already picked... You pretty much give yourself the answer in the phrasing of your question ^_^

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by KBriggs View Post
    If you have only 4 numbers, just pick a random one and make sure it is not the same as any you have already picked... You pretty much give yourself the answer in the phrasing of your question ^_^
    Thanks for the response, but how can i check it with the rest of the array???

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cosio55 View Post
    Thanks for the response, but how can i check it with the rest of the array???
    Compare it to values already in the array, pick a new number if you find a duplicate...

    Think loops and conditional tests...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this. It generates the number of random numbers you want, within the range of the numbers you want. If you want 10 random numbers, the range will be 0-9. If you want 100 random numbers, the range will be 0-99, etc.

    Each number it gives, is unique. Edit: but see the next post for a better algorithm.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 10
    
    int main() {
      int i, n; 
      int numbers[SIZE];
      time_t t;
      printf("\n\n\n");
      for(i=0;i<SIZE;i++) {  //initalize numbers
        numbers[i] = i;
      }
      srand((unsigned) time(&t));
      printf("\nUnique random numbers:\n");
      for(i=0;i<SIZE;i++) {
        n=rand() % SIZE; 
        if(numbers[n]<0)     //not unique, try again
          --i;
        else  {
          printf("%d\n", numbers[n]);   //got one
          numbers[n]=-1;     //mark that index taken, 
        }                    //thus marking that number
      }
    
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    Output:

    Unique random numbers:
    3
    8
    4
    6
    5
    7
    0
    1
    2
    9

    I just printed out the random numbers, but assigning them into another array, is nbd.
    Last edited by Adak; 10-28-2010 at 01:20 AM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Adak
    Only one comparison is needed.
    I'm not sure that's true, unless I'm missing your meaning. You say yourself in the code: "not unique, try again". The same index can be hit multiple times even though it's already been used.

    Adak's way works, but here's a solution that doesn't require the algorithm to "try again". This solution would have a huge performance gain over Adak's for a large set of numbers. For a set of 4 numbers like the OP wants it really doesn't matter.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 10
    
    int main(void)
    {
      int numbers[SIZE];
      int i, n, tmp;
    
      srand(time(NULL));
    
      // Initialize the array
      for(i = 0;i < SIZE;++i)
        numbers[i] = i;
    
      // Shuffle the array
      for(i = 0;i < SIZE;++i)
      {
        n = rand() % SIZE;
        tmp = numbers[n];
        numbers[n] = numbers[i];
        numbers[i] = tmp;
      }
    
      // Iterate through the array. Your numbers are already random
      for(i = 0;i < SIZE;++i)
        printf("%d\n", numbers[i]);
    
      return 0;
    }
    A couple of my outputs:

    Output 1:
    4
    1
    2
    9
    8
    6
    3
    5
    0
    7

    Output 2:
    7
    9
    0
    2
    6
    1
    3
    4
    8
    5
    Last edited by itsme86; 10-28-2010 at 12:26 AM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You nailed it, itsme86. It's been too long since I needed no repeat random numbers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    The core of itsme86's suggestion is to shuffle the array, but note that the shuffle used is biased. If that is undesirable, you could write:
    Code:
    /* Shuffle the array */
    for (i = SIZE; i > 1;)
    {
        n = rand() % i; /* ignore the possible slight bias introduced with this range reducing method */
        --i;
        tmp = numbers[n];
        numbers[n] = numbers[i];
        numbers[i] = tmp;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good catch, laserlight - as always.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-25-2010, 03:20 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  4. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 03:44 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21