CRandomHelper.h 655 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. class CRandomHelper
  3. {
  4. public:
  5. CRandomHelper();
  6. ~CRandomHelper();
  7. CRandomHelper(unsigned int seed)
  8. {
  9. next_seed = seed;
  10. }
  11. //生成0到1之间的随机数
  12. inline double lewaimai_rand()
  13. {
  14. return (double)myrand() / 0x7fff;
  15. }
  16. //生成范围内的随机整数
  17. inline int randInt(int begin, int end)
  18. {
  19. return begin + (myrand() % (end - begin + 1));
  20. }
  21. /* RAND_MAX assumed to be 32767 */
  22. inline int myrand(void)
  23. {
  24. next_seed = next_seed * 1103515245 + 12345;
  25. return((unsigned)(next_seed / 65536) % 32768);
  26. }
  27. inline void mysrand(unsigned int seed)
  28. {
  29. next_seed = seed;
  30. }
  31. private:
  32. unsigned long next_seed = 1;
  33. };