-
Notifications
You must be signed in to change notification settings - Fork 4
/
Rand.cpp
55 lines (43 loc) · 910 Bytes
/
Rand.cpp
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
54
#include "Arduino.h"
#include "Rand.h"
Rand::Rand()
{
x = 132456789;
y = 362436069;
z = 521288629;
}
uint32_t Rand::xorshift96()
{
uint32_t t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
void Rand::seed(uint32_t seed)
{
x = seed;
y = 362436069;
z = 521288629;
}
uint32_t Rand::random(uint32_t min, uint32_t max)
{
return (uint32_t) ((((xorshift96() & 0xFFFF) * (max-min))>>16) + min);
}
// Return a random number between 0 and max-1, inclusive
uint32_t Rand::random(uint32_t max)
{
return (uint32_t) (((xorshift96() & 0xFFFF) * max)>>16);
}
// Return a random number between 0 and 4095, inclusive
uint32_t Rand::random()
{
// Version #1: return (uint32_t) (((xorshift96() & 0xFFFF) * 4096)>>16);
// Version #2: return (uint32_t) (((xorshift96() & 0xFFFF)<<12)>>16);
// Version #3:
return (uint32_t) ((xorshift96() & 0xFFFF)>>4);
}