forked from ExpressLRS/ExpressLRS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
53 lines (45 loc) · 990 Bytes
/
utils.cpp
File metadata and controls
53 lines (45 loc) · 990 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
44
45
46
47
48
49
50
51
52
53
#include "utils.h"
unsigned long seed = 0;
// returns values between 0 and 0x7FFF
// NB rngN depends on this output range, so if we change the
// behaviour rngN will need updating
long rng(void)
{
unsigned long m = 2147483648;
long a = 214013;
long c = 2531011;
seed = (a * seed + c) % m;
return seed >> 16;
}
void rngSeed(long newSeed)
{
seed = newSeed;
}
// returns 0 <= x < max where max <= 256
// (actual upper limit is higher, but there is one and I haven't
// thought carefully about what it is)
unsigned int rngN(unsigned int max)
{
unsigned long x = rng();
unsigned int result = (x * max) / RNG_MAX;
return result;
}
// 0..255 returned
long rng8Bit(void)
{
return rng() & 0b11111111;
}
// 0..31 returned
long rng5Bit(void)
{
return rng() & 0b11111;
}
// 0..255 returned
long rng0to2(void)
{
int randomNumber = rng() & 0b11;
while(randomNumber == 3) {
randomNumber = rng() & 0b11;
}
return randomNumber;
}