r_is_odd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * r_is_odd.c
  3. *
  4. * Copyright 2018 Stephen Stengel <stephen.stengel@cwu.edu>
  5. * License: GPLv3
  6. * github: https://github.com/stephenstengel/r_is_odd/
  7. *
  8. */
  9. /**
  10. * DESCRIPTION: A joke function that returns whether or not an int is odd or
  11. * even. Also; some notes.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. int r_is_odd(int number_to_check);
  18. //******************************************************************************
  19. /**
  20. * r_is_odd() returns 0 if even and 1 if odd. Usually.
  21. */
  22. int r_is_odd(int number_to_check)
  23. {
  24. if (rand() % 100 < 1)
  25. {
  26. return rand() % 2;
  27. }
  28. return number_to_check % 2;
  29. }
  30. //******************************************************************************
  31. /**
  32. * This just calls the r_is_odd function to test it with a printf. Change the
  33. * chance to %100 < 100 to make sure it works right.
  34. *
  35. * I'm currently investigating the differences between srand() and srandom(),
  36. * and rand() and random(). Apparently random() is a unix thing. I haven't
  37. * looked into it. random() might not work on windows.
  38. *
  39. * I checked in the time man page and it says:
  40. * "The tloc argument is obsolescent and should always be NULL in new code.
  41. * When tloc is NULL, the call cannot fail."
  42. * tloc is the given name of the input parameter for time(). So I should just
  43. * use NULL. This is related to the 2038 epoch problem. The only use of tloc
  44. * would be saving the exact time that was used for the seed. But just before
  45. * and just after will be close enough for most anything I think.
  46. *
  47. * TODO: What is the difference between time(NULL) and time(&t) ?
  48. * 2019 Oct 17:
  49. * &t is the address of the variable t that was created by the
  50. * declaration: time_t t; It is whatever was in memory at the
  51. * place where t was assigned to.
  52. *
  53. * Difference between rand() random() ?
  54. * 2019 oct 17:
  55. * Aparrently, rand() and srand() are ANSI.
  56. * random() and srandom are unix.
  57. *https://stackoverflow.com/questions/18726102/what-difference-between-rand-and-random-functions
  58. *
  59. * Why do I get a compiler warning for using sleep() ?
  60. * 2019 oct 17:
  61. * Because the prototype for sleep() is in unistd.h and I did not
  62. * include it. It still worked though!
  63. */
  64. int main(int argc, char **argv)
  65. {
  66. //time_t t;
  67. //srand(time(&t));
  68. //srandom(time(&t));
  69. srand(time(NULL)); //the good version
  70. //srandom(time(NULL));
  71. int TEST_NUMBER_EVEN = 6;
  72. int TEST_NUMBER_ODD = 7;
  73. for (int i = 0; i < 1000; i++)
  74. {
  75. printf("%d %d\n", r_is_odd(TEST_NUMBER_EVEN), r_is_odd(TEST_NUMBER_ODD));
  76. }
  77. //sleep(3);
  78. printf("Current UNIX time in seconds: %lu\n", time(NULL));
  79. return 0;
  80. }