-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandom.h
More file actions
43 lines (38 loc) · 830 Bytes
/
Random.h
File metadata and controls
43 lines (38 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
/*
MINI VIRTUAL ANALOG SYNTHESIZER
Copyright 2014 Kenneth D. Miller III
Random Number Generator
*/
namespace Random
{
// random seed
extern unsigned int gSeed;
// set seed
inline void Seed(unsigned int aSeed)
{
gSeed = aSeed;
}
// random unsigned integer
inline unsigned int Int()
{
#if 1
// 32-bit xor-shift generator
// based on an implementation presented in a paper by George Marsaglia:
// http://www.jstatsoft.org/v08/i14/paper
gSeed ^= (gSeed << 13);
gSeed ^= (gSeed >> 17);
gSeed ^= (gSeed << 5);
#else
gSeed = 1664525L * gSeed + 1013904223L;
#endif
return gSeed;
}
// random uniform float
inline float Float()
{
union { float f; unsigned u; } floatint;
floatint.u = 0x3f800000 | (Int() >> 9);
return floatint.f - 1.0f;
}
}