-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrandom.c
More file actions
60 lines (54 loc) · 1.83 KB
/
random.c
File metadata and controls
60 lines (54 loc) · 1.83 KB
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
55
56
57
58
59
/*
* random.c
*
* http://interactive-matter.eu/
*
* This file is part of Blinken Button.
*
* Blinken Button is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Blinken Button is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Blinken Button. If not, see <http://www.gnu.org/licenses/>.
*
*
* Created on: 26.01.2010
*/
//include the definitions for our chip, like pins, ports & so on
#include <stdio.h>
//include our own definitions
#include "random.h"
static uint32_t RandomSeedA = 65537;
static uint32_t RandomSeedB = 12345;
/*
* To randomize the seed we simply read add up all memory content.
* This is no very good random routine. I would not use it for crypto stuff
* (where you need real randomness) but the memory content is random enough to
* start with a new animation each time.
* To generate a good randomness it is useful to do this as early as possible
* in the startup. Since after startup some bits in the memory can be flipped
* by accident.
*/
void
randomize_seed(void)
{
uint16_t *addr = 0;
for (addr = 0; addr < (uint16_t*)0xFFFF; addr++)
RandomSeedB += (*addr);
}
/*
* This generates a new random number of maximum 'max'
*/
unsigned int
get_random(unsigned int max)
{
RandomSeedA = 36969 * (RandomSeedA & 65535) + (RandomSeedA >> 16);
RandomSeedB = 18000 * (RandomSeedB & 65535) + (RandomSeedB >> 16);
return ((RandomSeedA << 16) + RandomSeedB) % max;
}