rand.c 803 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. // Uninitialized generator
  6. int rand_uninit = rand();
  7. printf("%d\n", rand_uninit);
  8. // Testing the reproducibility of values
  9. srand(259);
  10. int rand_seed259_1 = rand();
  11. printf("%d\n", rand_seed259_1);
  12. srand(259);
  13. int rand_seed259_2 = rand();
  14. printf("%d\n", rand_seed259_2);
  15. if (rand_seed259_1 != rand_seed259_2) {
  16. puts("rand() doesn't return reproducible values");
  17. exit(EXIT_FAILURE);
  18. }
  19. // Seed value 1 should return the same values as the ininitialized generator
  20. srand(1);
  21. int rand_seed1 = rand();
  22. printf("%d\n", rand_seed1);
  23. if (rand_uninit != rand_seed1) {
  24. puts("srand(1) doesn't work");
  25. exit(EXIT_FAILURE);
  26. }
  27. }