xorshiftrandom.c 896 B

1234567891011121314151617181920212223242526272829303132
  1. /*******************************************************************************
  2. * Copyright (c) 2017, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <time.h>
  7. #include "xorshiftrandom.h"
  8. static uint32_t xor_shift_seed; /** < File-global variable holding the current seed*/
  9. void SetXorShiftSeed(uint32_t seed) {
  10. xor_shift_seed = seed;
  11. }
  12. /** @brief Pseudo-random number algorithm
  13. * The algorithm used to create the pseudo-random numbers.
  14. * Works directly on the file global variable
  15. */
  16. void CalculateNextSeed(void) {
  17. if (xor_shift_seed == 0)
  18. SetXorShiftSeed(time(NULL));
  19. xor_shift_seed ^= xor_shift_seed << 13;
  20. xor_shift_seed ^= xor_shift_seed >> 17;
  21. xor_shift_seed ^= xor_shift_seed << 5;
  22. }
  23. uint32_t NextXorShiftUint32(void) {
  24. CalculateNextSeed();
  25. return xor_shift_seed;
  26. }