Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #define DEFAULT_SIZE //הגודל ברירת מחדל שאת רוצה
  5. static size_t uniques_size=0;
  6. static int* uniques_arr;
  7. static bool uniques_initialized=false;
  8.  
  9. void initialize_uniques(size_t size, int seed)
  10. {
  11. if (unqiues_initialized)
  12. unique_arr=realloc(unique_arr, sizeof(int)*size);
  13. else uniques_arr=calloc(size, sizeof(int));
  14. if (uniques_arr==NULL)
  15. {
  16. errno=ENOMEM;
  17. unique_initialized=false; //in case of reinitialization
  18. return;
  19. }
  20. srand(seed);
  21. for (int i=0; i<size; i++)
  22. uniques_arr[i] = i;
  23. uniques_initialized=true;
  24. }
  25.  
  26. void int_swap(int* a, int* b)
  27. {
  28. int temp=*a;
  29. *a=*b;
  30. *b=temp;
  31. }
  32. int generate_unique()
  33. {
  34. if (!unique_initialized||uniques_size==0)
  35. initialize_uniques(DEFAULT_SIZE, time(NULL)) ;
  36. swap(&uniques_arr[rand()%uniques_size-1], &uniques_arr[uniques_size-1]);
  37. uniques_size--;
  38. return uniques_arr[uniques_size] ;
  39. }
  40.  
  41.  
  42. int uniques_left()
  43. {
  44. return uniques_size;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement