/sparrowhawk/foundation/ESFRand.h
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 17#ifndef ESF_RAND_H 18#define ESF_RAND_H 19 20#ifndef ESF_CONFIG_H 21#include <ESFConfig.h> 22#endif 23 24#ifndef ESF_ALLOCATOR_H 25#include <ESFAllocator.h> 26#endif 27 28/** ESFRand generates pseudorandom numbers using the C Standard Library or 29 * POSIX.1 random number generator. 30 * 31 * @ingroup util 32 */ 33class ESFRand { 34public: 35 36 /** Default constructor. The seed is initialized to the current system 37 * time. 38 */ 39 ESFRand(); 40 41 /** Constructor. 42 * 43 * @param seed The seed for the random number generator 44 */ 45 ESFRand(unsigned int seed); 46 47 /** Default destructor. */ 48 virtual ~ESFRand(); 49 50 /** Generate a random number between 0.0 inclusive and 1.0 exclusive. 51 * 52 * @return A random number. 53 */ 54 double generateRandom(); 55 56 /** Generate a random integer within a given range. 57 * 58 * @param lowerBound The lowerbound of the range. Inclusive. 59 * @param upperBound The upperbound of the range. Inclusive. 60 * @return A random number within the given range. 61 */ 62 int generateRandom(int lowerBound, int upperBound); 63 64 /** Placement new. 65 * 66 * @param size The size of the object. 67 * @param allocator The source of the object's memory. 68 * @return The new object or NULL of the memory allocation failed. 69 */ 70 inline void *operator new(size_t size, ESFAllocator *allocator) { 71 return allocator->allocate(size); 72 } 73 74private: 75 76#ifdef HAVE_RAND_R 77 unsigned int _seed; 78#endif 79 80}; 81 82#endif /* ! ESF_RAND_H */