source: trunk/src/lol/math/rand.h @ 2526

Last change on this file since 2526 was 2526, checked in by sam, 8 years ago

math: don't use the LLU suffix for 64-bit numbers and don't use floorf().

File size: 3.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The Random number generators
13// ----------------------------
14//
15
16#if !defined __LOL_MATH_RAND_H__
17#define __LOL_MATH_RAND_H__
18
19#include <cstdlib>
20#include <stdint.h>
21
22namespace lol
23{
24
25/* Random number generators */
26template<typename T> static inline T rand();
27template<typename T> static inline T rand(T a);
28template<typename T> static inline T rand(T a, T b);
29
30/* One-value random number generators */
31template<typename T> static inline T rand(T a)
32{
33    return rand<T>() % a;
34}
35
36template<>
37inline half rand<half>(half a) { return a * std::rand() / RAND_MAX; }
38template<>
39inline float rand<float>(float a) { return a * std::rand() / RAND_MAX; }
40template<>
41inline double rand<double>(double a) { return a * std::rand() / RAND_MAX; }
42template<>
43inline ldouble rand<ldouble>(ldouble a) { return a * std::rand() / RAND_MAX; }
44
45/* Two-value random number generator -- no need for specialisation */
46template<typename T> static inline T rand(T a, T b)
47{
48    return a + rand<T>(b - a);
49}
50
51/* Default random number generator */
52template<typename T> static inline T rand()
53{
54    switch (sizeof(T))
55    {
56    case 1:
57        return static_cast<T>(std::rand() & 0x7f);
58    case 2:
59    {
60        uint16_t ret = std::rand();
61        if (RAND_MAX < 0x7fff)
62            ret = (ret << 7) ^ std::rand();
63        return static_cast<T>(ret & 0x7fffu);
64    }
65    case 4:
66    {
67        uint32_t ret = std::rand();
68        if (RAND_MAX >= 0xffff)
69            ret = (ret << 16) ^ std::rand();
70        else
71        {
72            ret = (ret << 8) ^ std::rand();
73            ret = (ret << 8) ^ std::rand();
74            ret = (ret << 8) ^ std::rand();
75        }
76        return static_cast<T>(ret & 0x7fffffffu);
77    }
78    case 8:
79    {
80        uint64_t ret = std::rand();
81        if (RAND_MAX >= 0xffff)
82        {
83            ret = (ret << 16) ^ std::rand();
84            ret = (ret << 16) ^ std::rand();
85            ret = (ret << 16) ^ std::rand();
86        }
87        else
88        {
89            ret = (ret << 8) ^ std::rand();
90            ret = (ret << 8) ^ std::rand();
91            ret = (ret << 8) ^ std::rand();
92            ret = (ret << 8) ^ std::rand();
93            ret = (ret << 8) ^ std::rand();
94            ret = (ret << 8) ^ std::rand();
95            ret = (ret << 8) ^ std::rand();
96        }
97        return static_cast<T>(ret & (~(uint64_t)0 >> 1));
98    }
99    default:
100        ASSERT(false, "rand() doesn’t support types of size %d\n",
101               (int)sizeof(T));
102        return 0;
103    }
104}
105
106template<> inline half rand<half>() { return rand<half>(1.f); }
107template<> inline float rand<float>() { return rand<float>(1.f); }
108template<> inline double rand<double>() { return rand<double>(1.0); }
109template<> inline ldouble rand<ldouble>() { return rand<ldouble>(1.0); }
110
111} /* namespace lol */
112
113#endif // __LOL_MATH_RAND_H__
114
Note: See TracBrowser for help on using the repository browser.