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.
Diff: random.cpp
- Revision:
- 2:0c241937eabd
- Parent:
- 0:f736749c33d2
- Child:
- 13:8ea85a33e37a
--- a/random.cpp Mon May 14 17:37:26 2018 +0000
+++ b/random.cpp Mon May 14 18:07:57 2018 +0000
@@ -5,12 +5,25 @@
namespace random
{
AnalogIn noise(RANDOM_NOISE_PIN);
+ bool initialized = false;
void init() {
srand(noise.read_u16());
+ initialized = true;
+ }
+
+ uint16_t unif(const uint16_t& nlevels) {
+ if (!initialized) {
+ init();
+ }
+ return ((uint16_t)rand()) % nlevels;
}
uint16_t exponential(const uint16_t& tau_ms, const uint16_t& cutoff, const unsigned int& resolution){
+ if (!initialized) {
+ init();
+ }
+
double ftau = (double)tau_ms;
double fcutoff = (double)cutoff;
double cumulativeFraction = ((double)(rand() & resolution))/resolution;