Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
Random.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2020 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #pragma comment(lib,"Winmm.lib")
27 
28 #include <Windows.h>
29 
30 #include "Random.h"
31 
33 // Constructor and initialization
34 
35 #pragma region Constructor and initialization
36 
39 
41  srand();
42 } //constructor
43 
49 
50 void CRandom::srand(int seed){
51  ::srand((seed >= 0)? seed: timeGetTime() & 0x7FFF);
52 
53  m_uState[0] = UINT(::rand());
54 
55  for(int i=1; i<=3; i++)
56  m_uState[i] = m_uState[i - 1]*UINT(::rand());
57 } //srand
58 
59 #pragma endregion Constructor and initialization
60 
62 // Functions that generate pseudo-random numbers
63 
64 #pragma region Generate pseudo-random numbers
65 
71 
73  UINT s = m_uState[3];
74 
75  s ^= s << 11;
76  s ^= s >> 8;
77 
78  m_uState[3] = m_uState[2];
79  m_uState[2] = m_uState[1];
80  m_uState[1] = m_uState[0];
81 
82  s ^= m_uState[0];
83  s ^= m_uState[0] >> 19;
84 
85  m_uState[0] = s;
86 
87  return s;
88 } //randn
89 
94 
95 UINT CRandom::randn(UINT i, UINT j){
96  return randn()%(j - i + 1) + i;
97 } //randn
98 
103 
105  return (float)randn()/(float)0xFFFFFFFF;
106 } //randf
107 
108 #pragma endregion Generate pseudo-random numbers
UINT m_uState[4]
Current state.
Definition: Random.h:38
CRandom()
Constructor.
Definition: Random.cpp:40
UINT randn()
Get random unsigned integer.
Definition: Random.cpp:72
float randf()
Get random floating point number.
Definition: Random.cpp:104
void srand(int seed=-1)
Seed the random number generator.
Definition: Random.cpp:50
Interface for the pseudorandom number generator CRandom.