zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Random.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
30 #ifndef ZENI_RANDOM_H
31 #define ZENI_RANDOM_H
32 
33 #include <cassert>
34 #include <cstdlib>
35 
36 #include <SDL/SDL_stdinc.h>
37 
38 namespace Zeni {
39 
40  class ZENI_DLL Random {
41  public:
42  Random(const Uint32 &seed = Uint32(std::rand()))
43  : m_random_value(seed)
44  {
45  }
46 
48  Sint32 rand_max() const {
49  return 32767;
50  }
51 
54  m_random_value = m_random_value * 1103515245 + 12345;
55  return Sint32(static_cast<Sint32>(m_random_value / 65536) % (rand_max() + 1));
56  }
57 
59  float frand_lt() {
60  return rand() / float(rand_max() + 1);
61  }
62 
64  float frand_lte() {
65  return rand() / float(rand_max());
66  }
67 
69  Sint32 rand_lt(const Sint32 &mod) {
70  assert(mod <= rand_max() + 1);
71  return Sint32(frand_lt() * mod);
72  }
73 
75  Sint32 rand_lte(const Sint32 &mod) {
76  assert(mod <= rand_max());
77  return Sint32(frand_lt() * (mod + 1));
78  }
79 
80  private:
81  Uint32 m_random_value;
82  };
83 
84 }
85 
86 #endif
Sint32 rand_lte(const Sint32 &mod)
Get a random integer in the range [0, mod].
Definition: Random.h:75
float frand_lte()
Get a random floating point number in the range [0.0f, 1.0f].
Definition: Random.h:64
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:141
Sint32 rand_lt(const Sint32 &mod)
Get a random integer in the range [0, mod)
Definition: Random.h:69
Sint32 rand()
Get a random integer in the range [0, rand_max()].
Definition: Random.h:53
#define assert(x)
Definition: SDL_malloc.c:1234
A Random Number Generator.
Definition: Random.h:40
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
float frand_lt()
Get a random floating point number in the range [0.0f, 1.0f)
Definition: Random.h:59
Random(const Uint32 &seed=Uint32(std::rand()))
Definition: Random.h:42
Sint32 rand_max() const
Get the maximum size of a random integer returned from rand()
Definition: Random.h:48