collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Joseph RyanAdd starter filesaa6d022

master
1.3 KiB50 linesraw
1// Random number generator
2// Linear congruential generator 
3// from Numerical Recipes by Press et al.
4// Jonathan Valvano
5
6// How to use: 
7// 1) call Random_Init once with a seed
8//     Random_Init(1);
9// or    Random_Init(NVIC_CURRENT_R);
10// 2) call Random over and over to get a new random number
11//   n = Random();    // 8 bit number
12//   m = (Random32()>>24)%60; // a number from 0 to 59
13
14#include <stdint.h>
15uint32_t static M;      
16
17// initialize random number generator
18// the generator cycles through all 32-bit values, seed is the place to start
19// input: any 32-bit number
20// output: none
21void Random_Init(uint32_t seed){
22  M= seed; //  Initial seed
23}
24
25//------------Random32------------
26// Return 32-bit random number
27// Linear congruential generator 
28// from Numerical Recipes by Press et al.
29// Input: none
30// Output: random number
31// warning: the bottom bits are not that randome
32//          bit 0 oscillates 0,1,0,1,...
33//          bit n cycles through a sequence of length n+1
34uint32_t Random32(void){
35  M = 1664525*M+1013904223;
36  return M;
37}
38
39//------------Random------------
40// Return random number, 0 to 255
41// Linear congruential generator 
42// from Numerical Recipes by Press et al.
43// Input: none
44// Output: random number
45uint8_t Random (void){
46  M = 1664525*M+1013904223;
47  return M&0xFF;
48}
49
50