rand.c 961 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. int libc_rand(void)
  13. {
  14. int i1, i2;
  15. int j1, j2;
  16. /* The C standard says that "If rand is called before any calls to
  17. srand have been made, the same sequence shall be generated as
  18. when srand is first called with a seed value of 1." */
  19. i1 = rand();
  20. i2 = rand();
  21. srand(1);
  22. j1 = rand();
  23. j2 = rand();
  24. if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0)
  25. {
  26. puts("Test FAILED!");
  27. }
  28. if (j1 == i1 && j2 == i2)
  29. {
  30. puts("Test succeeded.");
  31. return 0;
  32. }
  33. else
  34. {
  35. if (j1 != i1)
  36. printf("%d != %d\n", j1, i1);
  37. if (j2 != i2)
  38. printf("%d != %d\n", j2, i2);
  39. puts("Test FAILED!");
  40. return 1;
  41. }
  42. }