2020-05-17 04:40:08 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: Prandom.h
|
|
|
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
2022-11-22 08:03:23 -05:00
|
|
|
// VERSION: 0.1.5
|
2020-05-17 04:40:08 -04:00
|
|
|
// PURPOSE: Arduino library for random numbers with Python Random interface
|
2020-11-27 05:28:57 -05:00
|
|
|
// The underlying pseudo-random number generator is a
|
2020-05-17 04:40:08 -04:00
|
|
|
// Multiply-with-carry method invented by George Marsaglia.
|
2020-11-27 05:28:57 -05:00
|
|
|
// URL: https://github.com/RobTillaart/Prandom
|
2020-05-17 04:40:08 -04:00
|
|
|
// https://docs.python.org/3/library/random.html
|
|
|
|
// https://www.pcg-random.org/
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2020-05-17 04:40:08 -04:00
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2022-11-22 08:03:23 -05:00
|
|
|
#define PRANDOM_LIB_VERSION (F("0.1.5"))
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2020-05-17 04:40:08 -04:00
|
|
|
|
|
|
|
class Prandom
|
|
|
|
{
|
|
|
|
public:
|
2020-11-27 05:28:57 -05:00
|
|
|
Prandom();
|
|
|
|
Prandom(uint32_t s);
|
2020-05-17 04:40:08 -04:00
|
|
|
|
2020-11-27 05:28:57 -05:00
|
|
|
void seed();
|
2022-11-22 08:03:23 -05:00
|
|
|
void seed(uint32_t s, uint32_t t = 2); // Marsaglia need 2 seeds, but 1 will work too
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2020-05-17 04:40:08 -04:00
|
|
|
|
2022-11-22 08:03:23 -05:00
|
|
|
// integer methods
|
2020-11-27 05:28:57 -05:00
|
|
|
//
|
|
|
|
uint32_t getrandbits(uint8_t n);
|
|
|
|
uint32_t randrange(uint32_t stop);
|
|
|
|
uint32_t randrange(uint32_t start, uint32_t stop, uint32_t step = 1);
|
2022-11-22 08:03:23 -05:00
|
|
|
// randint is inclusive end value
|
2020-11-27 05:28:57 -05:00
|
|
|
uint32_t randint(uint32_t start, uint32_t stop) { return randrange(start, stop + 1); };
|
2020-05-17 04:40:08 -04:00
|
|
|
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2022-11-22 08:03:23 -05:00
|
|
|
// real distributions
|
2020-11-27 05:28:57 -05:00
|
|
|
//
|
|
|
|
float random(const float top = 1.0);
|
|
|
|
float uniform(float lo, float hi);
|
|
|
|
float triangular(float lo = 0, float hi = 1.0, float mid = 0.5);
|
|
|
|
float normalvariate(float mu = 0, float sigma = 1.0);
|
|
|
|
float lognormvariate(float mu = 0, float sigma = 1.0);
|
|
|
|
float gauss(float mu = 0, float sigma = 1.0);
|
|
|
|
float expovariate(float lambda);
|
|
|
|
float gammavariate(float alpha, float beta);
|
|
|
|
float betavariate(float alpha, float beta);
|
|
|
|
float paretovariate(float alpha);
|
|
|
|
float weibullvariate(float alpha, float beta);
|
2021-12-23 14:43:00 -05:00
|
|
|
|
|
|
|
|
2022-11-22 08:03:23 -05:00
|
|
|
// Circular distributions
|
2020-11-27 05:28:57 -05:00
|
|
|
//
|
2022-11-22 08:03:23 -05:00
|
|
|
// mu is mean angle in radians
|
|
|
|
// kappa is concentration parameter, 0 -> uniform.
|
2020-11-27 05:28:57 -05:00
|
|
|
float vonmisesvariate(float mu, float kappa = 0);
|
2020-05-17 04:40:08 -04:00
|
|
|
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2020-05-17 04:40:08 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
uint32_t _rndTime();
|
|
|
|
uint32_t _rnd(uint32_t n);
|
|
|
|
|
2022-11-22 08:03:23 -05:00
|
|
|
// Marsaglia 'constants'
|
2020-05-17 04:40:08 -04:00
|
|
|
uint32_t _m_w = 1;
|
2020-11-27 05:28:57 -05:00
|
|
|
uint32_t _m_z = 2;
|
2020-05-17 04:40:08 -04:00
|
|
|
uint32_t __random();
|
|
|
|
};
|
|
|
|
|
2021-12-23 14:43:00 -05:00
|
|
|
|
2020-05-17 04:40:08 -04:00
|
|
|
// -- END OF FILE --
|
2021-12-23 14:43:00 -05:00
|
|
|
|