zenilib: zenilib/jni/external/zenilib/zeni/Zeni/Random.h Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
53 Sint32 rand() {
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
Zeni::Random::rand_lte
Sint32 rand_lte(const Sint32 &mod)
Get a random integer in the range [0, mod].
Definition: Random.h:75
Zeni::Random::frand_lte
float frand_lte()
Get a random floating point number in the range [0.0f, 1.0f].
Definition: Random.h:64
Sint32
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:141
Zeni::Random::rand_lt
Sint32 rand_lt(const Sint32 &mod)
Get a random integer in the range [0, mod)
Definition: Random.h:69
Zeni::Random::rand
Sint32 rand()
Get a random integer in the range [0, rand_max()].
Definition: Random.h:53
assert
#define assert(x)
Definition: SDL_malloc.c:1234
Zeni::Random
A Random Number Generator.
Definition: Random.h:40
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
Zeni::Random::frand_lt
float frand_lt()
Get a random floating point number in the range [0.0f, 1.0f)
Definition: Random.h:59
Zeni::Random::Random
Random(const Uint32 &seed=Uint32(std::rand()))
Definition: Random.h:42
Zeni::Random::rand_max
Sint32 rand_max() const
Get the maximum size of a random integer returned from rand()
Definition: Random.h:48