sequence.h 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Sequence header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_SEQUENCE_H
  10. #define DORAYME_SEQUENCE_H
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <time.h>
  14. class Sequence
  15. {
  16. private:
  17. double *list;
  18. uint32_t listPos;
  19. uint32_t listSize;
  20. public:
  21. Sequence() : list(nullptr), listPos(0), listSize(0) {
  22. /* Need to bootstrap rand here */
  23. srand(time(NULL));
  24. }
  25. Sequence(double *list, uint32_t listSize) : list(list), listPos(0), listSize(listSize) { };
  26. static double frand(void)
  27. {
  28. return rand() / ((double) RAND_MAX);
  29. }
  30. double next() {
  31. if (this->listSize == 0)
  32. {
  33. return frand();
  34. }
  35. else
  36. {
  37. uint32_t pos = this->listPos;
  38. this->listPos = (this->listPos + 1) % this->listSize;
  39. return this->list[pos];
  40. }
  41. }
  42. };
  43. #endif /* DORAYME_SEQUENCE_H */