rand.c 479 B

12345678910111213141516171819202122232425262728293031
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simple xorshift PRNG
  4. * see http://www.jstatsoft.org/v08/i14/paper
  5. *
  6. * Copyright (c) 2012 Michael Walle
  7. * Michael Walle <michael@walle.cc>
  8. */
  9. #include <common.h>
  10. static unsigned int y = 1U;
  11. unsigned int rand_r(unsigned int *seedp)
  12. {
  13. *seedp ^= (*seedp << 13);
  14. *seedp ^= (*seedp >> 17);
  15. *seedp ^= (*seedp << 5);
  16. return *seedp;
  17. }
  18. unsigned int rand(void)
  19. {
  20. return rand_r(&y);
  21. }
  22. void srand(unsigned int seed)
  23. {
  24. y = seed;
  25. }