Advertisement
dmilicev

random_numbers_without_repeating.c

Apr 8th, 2020
447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None
  1. /*
  2.     random_numbers_without_repeating.c
  3.  
  4. Fills in the array with random numbers without repeating numbers.
  5.  
  6. Necessary code lines to generate random numbers:
  7.  
  8. #include<stdlib.h>  // RAND_MAX is defined in stdlib.h
  9. #include<time.h>    // for random number generator
  10.  
  11. in function main() :
  12.  
  13.     int number;
  14.     time_t t;   // for random number generator
  15.  
  16.     // Intializes random number generator, should only be called once.
  17.     srand((unsigned) time(&t));
  18.  
  19.     // function rand() returns an integer value between 0 and RAND_MAX.
  20.     number = rand();
  21.     // or
  22.     number = rand() % 9 + 1;    // generate random number between 1 and 9
  23.  
  24. */
  25.  
  26. #include<stdio.h>
  27. #include<stdlib.h>  // RAND_MAX is defined in stdlib.h
  28. #include<time.h>    // for random number generator
  29.  
  30. #define ARRAY_SIZE  100
  31.  
  32.  
  33. // generate and return random integer number between lower and upper, including them.
  34. int get_random_integer_from_lower_to_uppper(int lower, int upper)
  35. {
  36.     // generate and return random number between 0 and upper-1
  37.     //return( rand() % upper );
  38.  
  39.     // generate and return random number between 1 and upper
  40.     //return( rand() % upper + 1 );
  41.  
  42.     // generate and return random integer number between lower and upper
  43.     return( (rand() % (upper - lower + 1)) + lower );
  44. } // get_random_integer_from_lower_to_uppper()
  45.  
  46.  
  47. // Returns unique random integer between lower and upper,
  48. // different from numbers in array[] of n integers
  49. int get_unique_number( int array[], int n, int lower, int upper )
  50. {
  51.     int i, number, unique;
  52.  
  53.     if( (upper-lower)< n ) {
  54.         printf("\n Can't find %d unique elements from %d numbers ! \n", n+1, upper-lower+1);
  55.         exit(1);
  56.     }
  57.  
  58.     if( lower >= upper ) {
  59.         printf("\n Lower is greater than or equal than upper ! \n", n+1, upper-lower+1);
  60.         exit(1);
  61.     }
  62.  
  63.     while( 1 ) {
  64.         i=0;                        // reset i
  65.         unique = 1;                 // reset flag unique
  66.         number = get_random_integer_from_lower_to_uppper(lower, upper);
  67.  
  68.         while( i<n && unique ) {    // for all array elements, while unique!=0
  69.             if( array[i] == number ) {
  70.                 unique = 0;         // than number isn't unique in the array[]
  71.             }
  72.             i++;                    // move on to the next element of array[]
  73.         } // while( i<n && unique )
  74.  
  75.         if( unique )                // if number is unique in array[]
  76.             return number;
  77.         else                        // if it isn't unique, get another random integer
  78.             number = get_random_integer_from_lower_to_uppper(lower,upper);
  79.     } // while(1)
  80. } // get_unique_number()
  81.  
  82.  
  83.  
  84. int main(void)
  85. {
  86.     int i, n=ARRAY_SIZE, min=1, max=32000;
  87.     int array[ARRAY_SIZE];              // array for unique integers
  88.     time_t t;                           // for random number generator
  89.  
  90.     // Intializes random number generator, should only be called once.
  91.     srand((unsigned) time(&t));
  92.  
  93.     for(i=0; i<n; i++) {
  94.         array[i] = get_unique_number( array, i, min, max );
  95.     }
  96.  
  97.     printf("\n Array of %d random integers between %d and %d is: \n", n, min, max);
  98.  
  99.     for(i=0; i<n; i++) {                // print array on screen, 13 in one line
  100.  
  101.         if( i%13==0)
  102.             printf("\n");
  103.  
  104.         printf("%6d", array[i]);
  105.     }
  106.  
  107.     printf("\n");
  108.  
  109.     return(0);
  110. } // main()
Advertisement
RAW Paste Data Copied
Advertisement