/sparrowhawk/foundation/ESFRand.h

http://github.com/jtblatt/duderino · C Header · 82 lines · 24 code · 14 blank · 44 comment · 0 complexity · c331c09410598c2d5cc3bf8190ddc8af MD5 · raw file

  1. /** @file ESFRand.h
  2. * @brief A pseudo-random number generator
  3. *
  4. * Copyright (c) 2009 Yahoo! Inc.
  5. * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
  6. * under the BSD (revised) open source license.
  7. *
  8. * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
  9. * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
  10. *
  11. * $Author: blattj $
  12. * $Date: 2009/05/25 21:51:08 $
  13. * $Name: $
  14. * $Revision: 1.3 $
  15. */
  16. #ifndef ESF_RAND_H
  17. #define ESF_RAND_H
  18. #ifndef ESF_CONFIG_H
  19. #include <ESFConfig.h>
  20. #endif
  21. #ifndef ESF_ALLOCATOR_H
  22. #include <ESFAllocator.h>
  23. #endif
  24. /** ESFRand generates pseudorandom numbers using the C Standard Library or
  25. * POSIX.1 random number generator.
  26. *
  27. * @ingroup util
  28. */
  29. class ESFRand {
  30. public:
  31. /** Default constructor. The seed is initialized to the current system
  32. * time.
  33. */
  34. ESFRand();
  35. /** Constructor.
  36. *
  37. * @param seed The seed for the random number generator
  38. */
  39. ESFRand(unsigned int seed);
  40. /** Default destructor. */
  41. virtual ~ESFRand();
  42. /** Generate a random number between 0.0 inclusive and 1.0 exclusive.
  43. *
  44. * @return A random number.
  45. */
  46. double generateRandom();
  47. /** Generate a random integer within a given range.
  48. *
  49. * @param lowerBound The lowerbound of the range. Inclusive.
  50. * @param upperBound The upperbound of the range. Inclusive.
  51. * @return A random number within the given range.
  52. */
  53. int generateRandom(int lowerBound, int upperBound);
  54. /** Placement new.
  55. *
  56. * @param size The size of the object.
  57. * @param allocator The source of the object's memory.
  58. * @return The new object or NULL of the memory allocation failed.
  59. */
  60. inline void *operator new(size_t size, ESFAllocator *allocator) {
  61. return allocator->allocate(size);
  62. }
  63. private:
  64. #ifdef HAVE_RAND_R
  65. unsigned int _seed;
  66. #endif
  67. };
  68. #endif /* ! ESF_RAND_H */