SHARE
TWEET

unique_rng.c

Rummeris Nov 17th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define RAND_PERIOD 65536
  2. unsigned int *rand_array;
  3.  
  4. int init_rand(){
  5.     int i;
  6.     unsigned int r1, r2, temp;
  7.     rand_array = (unsigned int*) malloc(RAND_PERIOD*sizeof(unsigned int));
  8.    
  9.     for(i=0; i<RAND_PERIOD; i++)
  10.         rand_array[i] = i;
  11.    
  12.     // Shuffle array
  13.     for(i=0; i<RAND_PERIOD; i++){
  14.         r1 = rand() % RAND_PERIOD;
  15.         r2 = rand() % RAND_PERIOD;
  16.         temp = rand_array[r1];
  17.         rand_array[r1] = rand_array[r2];
  18.         rand_array[r2] = temp;
  19.     }
  20.     return 0;
  21. }
  22.  
  23. int destroy_rand(){
  24.     free(rand_array);
  25.     return 0;
  26. }
  27.  
  28. unsigned int unique_random_number(){
  29.     static int rand_array_index=0;
  30.     unsigned int temp;
  31.     temp = rand_array[rand_array_index];
  32.     rand_array_index++;
  33.     return temp;
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top