Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
random.cpp
- Committer:
- gwappa
- Date:
- 2018-06-25
- Revision:
- 13:8ea85a33e37a
- Parent:
- 2:0c241937eabd
File content as of revision 13:8ea85a33e37a:
#include "random.h"
#include "mbed.h"
#include <limits.h>
namespace random
{
AnalogIn noise(RANDOM_NOISE_PIN);
bool initialized = false;
void init() {
srand(((uint32_t)noise.read_u16())<<16 & ((uint32_t)noise.read_u16()));
initialized = true;
}
uint32_t unif(const uint32_t& nlevels) {
if (!initialized) {
init();
}
return ((uint32_t)rand()) % nlevels;
}
uint32_t exponential(const uint32_t& tau, const uint32_t& cutoff, const unsigned int& resolution){
if (!initialized) {
init();
}
double ftau = (double)tau;
double fcutoff = (double)cutoff;
double cumulativeFraction = ((double)(rand() & resolution))/resolution;
// calculate the point (in exponential distribution) from the inverse of its cumulative density function
double point = -ftau * log(1-cumulativeFraction);
// return as uint32 value.
// if `point` is unreasonably long, then return `cutoff` instead.
if (point > fcutoff) {
return cutoff;
} else {
return (uint32_t)(point+0.5);
}
}
}