Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / src / synfig / quick_rng.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file quick_rng.h
3 **      \brief Template Header
4 **
5 **      $Id: guid.h 335 2007-03-16 00:39:09Z dooglus $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_QUICK_RNG_H
27 #define __SYNFIG_QUICK_RNG_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <stdint.h>
32
33 /* === M A C R O S ========================================================= */
34
35 /* === T Y P E D E F S ===================================================== */
36
37 /* === C L A S S E S & S T R U C T S ======================================= */
38
39 // A fast 32-bit linear congruential random number generator
40 class quick_rng
41 {
42         uint32_t next;
43 public:
44         quick_rng(uint32_t seed=0):next(seed) { }
45
46         void set_seed(uint32_t x)
47         {
48                 next=x;
49         }
50
51         uint32_t i32()
52         {
53                 static const uint32_t a(1664525);
54                 static const uint32_t c(1013904223);
55
56                 return next=next*a+c;
57         }
58
59         uint32_t i16()
60         {
61                 return i32()>>16;
62         }
63
64         float f()
65         {
66                 static const float m(int(65535));
67
68                 return float(i16())/m;
69         }
70
71         uint32_t operator()(const uint32_t& m)
72         {
73                 if(m==65536)
74                         return i16();
75                 else
76                 if(m<=65536)
77                         return i16()%m;
78                 else
79                         return i32()%m;
80         }
81 };
82
83 /* === E N D =============================================================== */
84
85 #endif