Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1/**************************************************************************\
2* *
3* Middle Square Weyl Sequence Random Number Generator *
4* *
5* msws() - returns a 32 bit unsigned int [0,0xffffffff] *
6* *
7* The state vector consists of three 64 bit words: x, w, and s *
8* *
9* x - random output [0,0xffffffff] *
10* w - Weyl sequence (period 2^64) *
11* s - an odd constant *
12* *
13* The Weyl sequence is added to the square of x. The middle is extracted *
14* by shifting right 32 bits: *
15* *
16* x *= x; x += (w += s); return x = (x>>32) | (x<<32); *
17* *
18* The constant s should be set to a random 64-bit pattern. The utility *
19* init_rand_digits in init.h may be used to initialize the constant. *
20* This utility generates constants with different hexadecimal digits. *
21* This assures sufficient change in the Weyl sequence on each iteration *
22* of the RNG. *
23* *
24* Note: This version of the RNG includes an idea proposed by *
25* Richard P. Brent (creator of the xorgens RNG). Brent suggested *
26* adding the Weyl sequence after squaring instead of before squaring. *
27* This provides a basis for uniformity in the output. *
28* *
29* Copyright (c) 2014-2020 Bernard Widynski *
30* *
31* This code can be used under the terms of the GNU General Public License *
32* as published by the Free Software Foundation, either version 3 of the *
33* License, or any later version. See the GPL license at URL *
34* http://www.gnu.org/licenses *
35* *
36\**************************************************************************/
37
38#pragma once
39
40#include <stdint.h>
41#include <third/random/state.h>
42
43namespace Msws {
44
45State GlobalMswsState = {.x = 0, .w = 0, .s = 0xb5ad4eceda1ce2a9};
46
50inline static uint32_t msws(State& state) {
51 state.x *= state.x;
52 state.x += (state.w += state.s);
53 return state.x = (state.x >> 32) | (state.x << 32);
54}
55
59inline static uint32_t msws() {
60 return msws(GlobalMswsState);
61}
62
63} // namespace Msws
Wrapper class for internal state of the Msws generator.
Definition: state.h:17
uint64_t x
Definition: state.h:19
uint64_t w
Definition: state.h:20
uint64_t s
Definition: state.h:21
Definition: random.h:43
static uint32_t msws()
Generate new pseudo random number from global state variables and update them.
Definition: random.h:59
State GlobalMswsState
Definition: random.h:45