From b90493571b92aa37d4cbf00389c2f06bf67d7fd3 Mon Sep 17 00:00:00 2001 From: Frank Holtz Date: Sun, 6 Nov 2016 14:04:42 +0100 Subject: [PATCH] TrueRandom: This is an enhancement of https://github.com/sirleech/TrueRandom Contents: - Support for nRF5 and avr platform - New README.md, replaces readme - Fixed Example AllFunctions --- .travis.yml | 3 + libraries/TrueRandom/README.md | 143 ++++++++++ libraries/TrueRandom/TrueRandom.cpp | 259 ++++++++++++++++++ libraries/TrueRandom/TrueRandom.h | 36 +++ .../examples/AllFunctions/AllFunctions.ino | 125 +++++++++ .../examples/Benchmark/Benchmark.ino | 48 ++++ libraries/TrueRandom/examples/Die/Die.ino | 33 +++ .../examples/MacAddress/MacAddress.ino | 70 +++++ .../examples/Magic8Ball/Magic8Ball.ino | 59 ++++ .../examples/SetRandomSeed/SetRandomSeed.ino | 28 ++ libraries/TrueRandom/examples/Uuid/Uuid.ino | 58 ++++ libraries/TrueRandom/keywords.txt | 26 ++ libraries/TrueRandom/library.properties | 9 + libraries/TrueRandom/release notes.txt | 7 + 14 files changed, 904 insertions(+) create mode 100644 libraries/TrueRandom/README.md create mode 100644 libraries/TrueRandom/TrueRandom.cpp create mode 100644 libraries/TrueRandom/TrueRandom.h create mode 100644 libraries/TrueRandom/examples/AllFunctions/AllFunctions.ino create mode 100644 libraries/TrueRandom/examples/Benchmark/Benchmark.ino create mode 100644 libraries/TrueRandom/examples/Die/Die.ino create mode 100644 libraries/TrueRandom/examples/MacAddress/MacAddress.ino create mode 100644 libraries/TrueRandom/examples/Magic8Ball/Magic8Ball.ino create mode 100644 libraries/TrueRandom/examples/SetRandomSeed/SetRandomSeed.ino create mode 100644 libraries/TrueRandom/examples/Uuid/Uuid.ino create mode 100644 libraries/TrueRandom/keywords.txt create mode 100644 libraries/TrueRandom/library.properties create mode 100644 libraries/TrueRandom/release notes.txt diff --git a/.travis.yml b/.travis.yml index a588d9b6..1ee80a3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ before_install: - export PATH=$PATH:$HOME/arduino-ide - arduino --pref "boardsmanager.additional.urls=https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json" --install-boards sandeepmistry:nRF5 > /dev/null - buildExampleSketch() { arduino --verbose-build --verify --board $1 $HOME/arduino-ide/examples/$2/$3/$3.ino; } + - buildNRF5Sketch() { arduino --verbose-build --verify --board $1 $HOME/Arduino/hardware/sandeepmistry/nRF5/libraries/$2/examples/$3/$3.ino; } install: - mkdir -p $HOME/Arduino/hardware/sandeepmistry - ln -s $PWD $HOME/Arduino/hardware/sandeepmistry/nRF5 @@ -32,3 +33,5 @@ script: - buildExampleSketch sandeepmistry:nRF5:STCT_nRF52_minidev 01.Basics Blink - buildExampleSketch sandeepmistry:nRF5:PCA1000X:board_variant=pca10000 01.Basics Blink - buildExampleSketch sandeepmistry:nRF5:PCA1000X:board_variant=nrf6310 01.Basics Blink + - buildNRF5Sketch sandeepmistry:nRF5:Generic_nRF51822:chip=xxac TrueRandom AllFunctions + - buildNRF5Sketch sandeepmistry:nRF5:Generic_nRF52832 TrueRandom AllFunctions diff --git a/libraries/TrueRandom/README.md b/libraries/TrueRandom/README.md new file mode 100644 index 00000000..a3735e9b --- /dev/null +++ b/libraries/TrueRandom/README.md @@ -0,0 +1,143 @@ +Introduction +============ + +TrueRandom generates true random numbers on Arduino. They are different every +time you start your program, and are truly unpredictable unlike the default +Arduino random() function. + +Compatibility +============= + +TrueRandom currently functions on the Arduino Diecimila, Duemilanove, 168 and +328 based Arduinos and Nordics nRF5 platform. It does not yet function on the +Arduino Mega. + +On patforms without a Hardware Random Number Generator, TrueRandom uses Analog 0. +Do not connect anything to this pin. These restrictions may be removed in future +versions of this library. + +On Nordiv nRF5 platforms the internal Hardware Number Generator (RNG) is used +for random number generation. The "Bias Correction" is enabled to provide better +random numbers. + +Generating a random byte takes a maximum of 250uS with nRF52 series and 677uS +with nRF51 series MCU. + +What happens when you use the Arduino random() function? +======================================================== + +The Arduino default random() function generates what appear to be random +numbers. They are actually calculated from a formula. On reset, the formula +is reset at a start point, then progresses through a long sequence of random +looking numbers. However, Arduino starts at the same point in the sequence +every reset. You can move to a different part of the sequence using srandom(), +but how do you get a random start point from in the first place? + +What happens when you use TrueRandom.random() function? +======================================================= + +You get a random number. Really random. Different every time you restart. + +Example time +============ + +```C +#include + +void setup() { + Serial.begin(9600); + + Serial.print("I threw a random die and got "); + Serial.print(random(1,7)); + + Serial.print(". Then I threw a TrueRandom die and got "); + Serial.println(TrueRandom.random(1,7)); + +} + +void loop() { + ; // Do nothing +} +``` + +Upload that code to an nRF5 and watch it on the Serial Monitor at 9600 baud. +Hit the reset button, and see what it does. The random() function returns the +same value every time, but the TrueRandom version is always different. + +TrueRandom basic functions +========================== + +The existing random functions of Arduino are replicated in TrueRandom. + +_TrueRandom.random()_ +Like the Arduino library and ANSI C, this generates a random number between 0 +and the highest signed long integer 2,147,483,647. + +_TrueRandom.random(n)_ +This generates a random number between 0 and (n-1). So random(6) will generate +numbers between 0 and 5. + +_TrueRandom.random(a,b)_ +This generates a random number between a and (b-1). So random(1,7) will generate +numbers between 1 and 6. + +TrueRandom advanced functions +============================= + +_TrueRandom.randomBit()_ +Generating true random numbers takes time, so it can be useful to only generate +as many random bits as you need. randomBit() generates a 0 or a 1 with 50% +probability. This is the core function from which the other TrueRandom libraries +are built. + +_TrueRandom.randomByte()_ +Generates a random byte between 0 and 255. Equivalent to random(256). + +_TrueRandom.rand()_ +Like the ANSI C rand() command, this generates a random number between 0 and the +highest signed integer 32767. + +_TrueRandom.memfill(address, length)_ +Fills a block of bytes with random numbers. (length) bytes are filled in total, +starting at the given (address). + +TrueRandom specialist functions +=============================== + +_TrueRandom.mac(address)_ +When operating devices on an Ethernet network, each device must have a unique +MAC address. Officially, MAC addresses should be assigned formally via the IEEE +Registration Authority. However, for practical purposes, MAC addresses can be +randomly assigned without problems. This function writes a 6 byte MAC address +to a given address. Randomly generated MAC addresses are great for projects or +workshops involving large numbers of Arduino Ethernet shields, as each shield +has a different MAC address, even though they are running identical code. See +the MacAddress example which shows this in use. + +_TrueRandom.uuid(address)_ +UUIDs are unique identifiers. They are 16 bytes (128 bits) long, which means +that generating them randomly This generates a random UUID, and writes it to +an array. UUIDs are globally unique numbers that are often used in web services +and production electronics. TrueRandom can produce any one of +5,316,911,983,139,663,491,615,228,241,121,378,304 different numbers. You're more +likely to win top prize in the national lottery 3 times in a row than get two +matching UUIDs. + +How TrueRandom works +==================== + +It is hard to get a truly random number from Arduino. TrueRandom does it by +setting up a noisy voltage on Analog pin 0, measuring it, and then discarding +all but the least significant bit of the measured value. However, that isn't +noisy enough, so a von Neumann whitening algorithm gathers enough entropy from +multiple readings to ensure a fair distribution of 1s and 0s. + +The other functions within TrueRandom construct the requested values by +gathering just enough random bits to produce the required numbers. Generating a +random bit takes time, so a significant part of the code works to ensure the +random bits are used as efficiently as possible. + +Projects using TrueRandom +========================= + +Generative Music from Gijs diff --git a/libraries/TrueRandom/TrueRandom.cpp b/libraries/TrueRandom/TrueRandom.cpp new file mode 100644 index 00000000..61fed0f1 --- /dev/null +++ b/libraries/TrueRandom/TrueRandom.cpp @@ -0,0 +1,259 @@ +/** + * TrueRandom - A true random number generator for Arduino. + * + * Copyright (c) 2010 Peter Knight, Tinker.it! All rights reserved. + */ + +#include "TrueRandom.h" + +#ifdef ARDUINO_ARCH_NRF5 +#include +/* Support for Nordic nRF5 Platform */ +void TrueRandomClass::startRNG(void) { + #ifdef NRF51 + NRF_RNG->POWER = 1; + #endif + NRF_RNG->TASKS_START = 1; + NRF_RNG->EVENTS_VALRDY = 0; +} + +void TrueRandomClass::stopRNG(void) { + NRF_RNG->TASKS_STOP = 1; + #ifdef NRF51 + NRF_RNG->POWER = 0; + #endif +} +char TrueRandomClass::readByte() { + char ret; + while (NRF_RNG->EVENTS_VALRDY == 0) { + yield(); + } + ret = (char)NRF_RNG->VALUE; + NRF_RNG->EVENTS_VALRDY = 0; + return ret; +} + +int TrueRandomClass::randomBit(void) { + // read a new byte when bitpos is 0 + if (this->random_bit_buffer_pos == 0) { + this->random_bit_buffer_pos = 1; + this->random_bit_buffer = this->randomByte(); + } + + int ret = (this->random_bit_buffer & this->random_bit_buffer_pos) > 0; + this->random_bit_buffer_pos = this->random_bit_buffer_pos << 1; + return (int)ret; +} + +char TrueRandomClass::randomByte(void) { + char ret; + this->startRNG(); + ret = this->readByte(); + this->stopRNG(); + return ret; +} + +int TrueRandomClass::rand() { + int result; + this->memfill((char*)&result,sizeof(result)); + return result; +} + +long TrueRandomClass::random() { + long result; + this->memfill((char*)&result,sizeof(result)); + return result; +} + +void TrueRandomClass::memfill(char* location, int size) { + startRNG(); + for (;size--;) { + *location++ = readByte(); + } + stopRNG(); +} + +// End ARDUINO_ARCH_NRF5 +#else +/* Support for Arduino Platform */ +#include +int TrueRandomClass::randomBitRaw(void) { + uint8_t copyAdmux, copyAdcsra, copyAdcsrb, copyPortc, copyDdrc; + uint16_t i; + uint8_t bit; + volatile uint8_t dummy; + + // Store all the registers we'll be playing with + copyAdmux = ADMUX; + copyAdcsra = ADCSRA; + copyAdcsrb = ADCSRB; + copyPortc = PORTC; + copyDdrc = DDRC; + + // Perform a conversion on Analog0, using the Vcc reference + ADMUX = _BV(REFS0); + +#if F_CPU > 16000000 + // ADC is enabled, divide by 32 prescaler + ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS0); +#elif F_CPU > 8000000 + // ADC is enabled, divide by 16 prescaler + ADCSRA = _BV(ADEN) | _BV(ADPS2); +#else + // ADC is enabled, divide by 8 prescaler + ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); +#endif + + // Autotriggering disabled + ADCSRB = 0; + + // Pull Analog0 to ground + PORTC &=~_BV(0); + DDRC |= _BV(0); + // Release Analog0, apply internal pullup + DDRC &= ~_BV(0); + PORTC |= _BV(1); + // Immediately start a sample conversion on Analog0 + ADCSRA |= _BV(ADSC); + // Wait for conversion to complete + while (ADCSRA & _BV(ADSC)) PORTC ^= _BV(0); + // Xor least significant bits together + bit = ADCL; + // We're ignoring the high bits, but we have to read them before the next conversion + dummy = ADCH; + + // Restore register states + ADMUX = copyAdmux; + ADCSRA = copyAdcsra; + ADCSRB = copyAdcsrb; + PORTC = copyPortc; + DDRC = copyDdrc; + + return bit & 1; +} + +int TrueRandomClass::randomBitRaw2(void) { + // Software whiten bits using Von Neumann algorithm + // + // von Neumann, John (1951). "Various techniques used in connection + // with random digits". National Bureau of Standards Applied Math Series + // 12:36. + // + for(;;) { + int a = randomBitRaw() | (randomBitRaw()<<1); + if (a==1) return 0; // 1 to 0 transition: log a zero bit + if (a==2) return 1; // 0 to 1 transition: log a one bit + // For other cases, try again. + } +} + +int TrueRandomClass::randomBit(void) { + // Software whiten bits using Von Neumann algorithm + // + // von Neumann, John (1951). "Various techniques used in connection + // with random digits". National Bureau of Standards Applied Math Series + // 12:36. + // + for(;;) { + int a = randomBitRaw2() | (randomBitRaw2()<<1); + if (a==1) return 0; // 1 to 0 transition: log a zero bit + if (a==2) return 1; // 0 to 1 transition: log a one bit + // For other cases, try again. + } +} + +char TrueRandomClass::randomByte(void) { + char result; + uint8_t i; + result = 0; + for (i=8; i--;) result += result + randomBit(); + return result; +} + +int TrueRandomClass::rand() { + int result; + uint8_t i; + result = 0; + for (i=15; i--;) result += result + randomBit(); + return result; +} + +long TrueRandomClass::random() { + long result; + uint8_t i; + result = 0; + for (i=31; i--;) result += result + randomBit(); + return result; +} + +void TrueRandomClass::memfill(char* location, int size) { + for (;size--;) *location++ = randomByte(); +} + +// Arduino platform +#endif + +long TrueRandomClass::random(long howBig) { + long randomValue; + long maxRandomValue; + long topBit; + long bitPosition; + + if (!howBig) return 0; + randomValue = 0; + if (howBig & (howBig-1)) { + // Range is not a power of 2 - use slow method + topBit = howBig-1; + topBit |= topBit>>1; + topBit |= topBit>>2; + topBit |= topBit>>4; + topBit |= topBit>>8; + topBit |= topBit>>16; + topBit = (topBit+1) >> 1; + + bitPosition = topBit; + do { + // Generate the next bit of the result + if (randomBit()) randomValue |= bitPosition; + + // Check if bit + if (randomValue >= howBig) { + // Number is over the top limit - start again. + randomValue = 0; + bitPosition = topBit; + } else { + // Repeat for next bit + bitPosition >>= 1; + } + } while (bitPosition); + } else { + // Special case, howBig is a power of 2 + bitPosition = howBig >> 1; + while (bitPosition) { + if (randomBit()) randomValue |= bitPosition; + bitPosition >>= 1; + } + } + return randomValue; +} + +long TrueRandomClass::random(long howSmall, long howBig) { + if (howSmall >= howBig) return howSmall; + long diff = howBig - howSmall; + return TrueRandomClass::random(diff) + howSmall; +} + +void TrueRandomClass::mac(uint8_t* macLocation) { + memfill((char*)macLocation,6); +} + +void TrueRandomClass::uuid(uint8_t* uuidLocation) { + // Generate a Version 4 UUID according to RFC4122 + memfill((char*)uuidLocation,16); + // Although the UUID contains 128 bits, only 122 of those are random. + // The other 6 bits are fixed, to indicate a version number. + uuidLocation[6] = 0x40 | (0x0F & uuidLocation[6]); + uuidLocation[8] = 0x80 | (0x3F & uuidLocation[8]); +} + +TrueRandomClass TrueRandom; diff --git a/libraries/TrueRandom/TrueRandom.h b/libraries/TrueRandom/TrueRandom.h new file mode 100644 index 00000000..6b8d9cda --- /dev/null +++ b/libraries/TrueRandom/TrueRandom.h @@ -0,0 +1,36 @@ +/** + * TrueRandom - A true random number generator for Arduino. + * + * Copyright (c) 2010 Peter Knight, Tinker.it! All rights reserved. + * Copyright (c) 2016 Frank Holtz, Tinker.it! All rights reserved. + */ + +#ifndef TrueRandom_h +#define TrueRandom_h + +#include +class TrueRandomClass +{ + public: + int rand(); + long random(); + long random(long howBig); + long random(long howsmall, long how); + int randomBit(void); + char randomByte(void); + void memfill(char* location, int size); + void mac(uint8_t* macLocation); + void uuid(uint8_t* uuidLocation); + private: + char random_bit_buffer, random_bit_buffer_pos=0; + #ifdef ARDUINO_ARCH_NRF5 + void startRNG(void); + void stopRNG(void); + char readByte(); + #else + int randomBitRaw(void); + int randomBitRaw2(void); + #endif +}; +extern TrueRandomClass TrueRandom; +#endif diff --git a/libraries/TrueRandom/examples/AllFunctions/AllFunctions.ino b/libraries/TrueRandom/examples/AllFunctions/AllFunctions.ino new file mode 100644 index 00000000..ba8a3250 --- /dev/null +++ b/libraries/TrueRandom/examples/AllFunctions/AllFunctions.ino @@ -0,0 +1,125 @@ +/* + * A magic 8 ball. + * + * Press the reset button to see into the future. + * + * View the answer to your question in the Serial Monitor, at 19200 baud. + * + * Press the Arduino reset button to ask another question. + * + */ + +#include + +char array[10]; +int arrayLength = 10; + +uint8_t macAddress[6]; // MAC addresses are always 6 bytes long +uint8_t uuidNumber[16]; // UUIDs are always 16 bytes long + +void printHex(char number) { + // Print high hex digit + Serial.print( "0123456789ABCDEF"[number / 16] ); + // Low hex digit + Serial.print( "0123456789ABCDEF"[number & 15] ); +} + +void printMac(uint8_t* macAddress) { + // Print a MAC address in the form + // 12:23:34:45:56:67 + int i; + for (i=0; i<6; i++) { + printHex(macAddress[i]); + if (i<5) Serial.print(":"); + } +} +void printUuid(uint8_t* uuidNumber) { + // Print a UUID in the form + // 12345678-1234-1234-1234-123456789ABC + int i; + for (i=0; i<16; i++) { + if (i==4) Serial.print("-"); + if (i==6) Serial.print("-"); + if (i==8) Serial.print("-"); + if (i==10) Serial.print("-"); + printHex(uuidNumber[i]); + } +} + +void setup() { + Serial.begin(9600); + + // TrueRandom.rand() returns a positive integer + // in the range 0..32767 + + Serial.print("A random int: "); + Serial.println(TrueRandom.rand()); + + // TrueRandom.random() returns a positive long integer + // in the range 0..2,147,483,647 + + Serial.print("A random long int: "); + Serial.println(TrueRandom.random()); + + // TrueRandom.random(n) returns a positive long integer + // in the range 0 .. (n-1) + + Serial.print("A random number between 0 and 999: "); + Serial.println(TrueRandom.random(1000)); + + // TrueRandom.random(a,b) returns a positive long integer + // in the range a .. (b-1). + // b must be larger than a. + + Serial.print("A random number between 1000 and 9999: "); + Serial.println(TrueRandom.random(1000,10000)); + + // TrueRandom.randomBit() returns a single random bit + // It returns either 1 or 0. + Serial.print("A random bit: "); + Serial.println(TrueRandom.randomBit()); + + // TrueRandom.randomByte() returns a single random byte + // It returns an 8 bit char (byte) in the range -128 to 127. + Serial.print("A random byte: "); + Serial.println(TrueRandom.randomByte(),DEC); + + // Zero an array + for(int i=0; i + +unsigned long startTime; +int i; + +void setup() { + Serial.begin(9600); + + Serial.println("TrueRandom benchmark"); + Serial.println("--------------------"); + Serial.println(); + + Serial.print("Arduino clock speed: "); + Serial.print(F_CPU/1000000); + Serial.println("MHz"); + + Serial.print("randomBit(): "); + startTime = millis(); + TrueRandom.randomBit(); + Serial.print(millis() - startTime); + Serial.println("ms"); + + Serial.print("randomByte(): "); + startTime = millis(); + TrueRandom.randomByte(); + Serial.print(millis() - startTime); + Serial.println("ms"); + + Serial.print("random(100): "); + startTime = millis(); + TrueRandom.random(100); + Serial.print(millis() - startTime); + Serial.println("ms"); + + Serial.print("random(65536): "); + startTime = millis(); + TrueRandom.random(65536); + Serial.print(millis() - startTime); + Serial.println("ms"); + + Serial.print("random(65537): "); + startTime = millis(); + TrueRandom.random(65537); + Serial.print(millis() - startTime); + Serial.println("ms"); +} +void loop() { +} \ No newline at end of file diff --git a/libraries/TrueRandom/examples/Die/Die.ino b/libraries/TrueRandom/examples/Die/Die.ino new file mode 100644 index 00000000..cb67888f --- /dev/null +++ b/libraries/TrueRandom/examples/Die/Die.ino @@ -0,0 +1,33 @@ +/* + * A simple electronic die. + * + * Press the reset button to throw a set of dice. + * + */ + +#include + +void setup() { + Serial.begin(9600); + Serial.println("Throwing..."); + delay(1000); + + Serial.print("6 sided die: "); + Serial.println(TrueRandom.random(1,7)); + Serial.print("4 sided die: "); + Serial.println(TrueRandom.random(1,5)); + Serial.print("8 sided die: "); + Serial.println(TrueRandom.random(1,9)); + Serial.print("10 sided die: "); + Serial.println(TrueRandom.random(1,11)); + Serial.print("12 sided die: "); + Serial.println(TrueRandom.random(1,13)); + Serial.print("20 sided die: "); + Serial.println(TrueRandom.random(1,21)); + Serial.print("100 sided die: "); + Serial.println(TrueRandom.random(1,101)); +} + +void loop() { + ; // Do nothing +} \ No newline at end of file diff --git a/libraries/TrueRandom/examples/MacAddress/MacAddress.ino b/libraries/TrueRandom/examples/MacAddress/MacAddress.ino new file mode 100644 index 00000000..fc68ba71 --- /dev/null +++ b/libraries/TrueRandom/examples/MacAddress/MacAddress.ino @@ -0,0 +1,70 @@ +// This example modifies the Ethernet Client example to use +// a randomly generated MAC address +// +// This has the advantage that no MAC addresses will clash, even if +// every device uses exactly the same code on its Arduino. +// + +// This example is identical to the Ethernet/Client example +// with three changes to utilise TrueRandom. + +#include + +// +// Change 1/3: Include TrueRandom library +#include +// +// + +// +// Change 2/3: Include TrueRandom library +// TrueRandom sets this, so no default is needed here. +byte mac[6]; +// +// + +byte ip[] = { 10, 0, 0, 177 }; +byte server[] = { 64, 233, 187, 99 }; // Google + +Client client(server, 80); + +void setup() +{ + // + // Change 3/3: Set a random MAC address using TrueRandom + TrueRandom.mac(mac); + // + // + + + Ethernet.begin(mac, ip); + Serial.begin(9600); + + delay(1000); + + Serial.println("connecting..."); + + if (client.connect()) { + Serial.println("connected"); + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } else { + Serial.println("connection failed"); + } +} + +void loop() +{ + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + for(;;) + ; + } +} \ No newline at end of file diff --git a/libraries/TrueRandom/examples/Magic8Ball/Magic8Ball.ino b/libraries/TrueRandom/examples/Magic8Ball/Magic8Ball.ino new file mode 100644 index 00000000..101bd385 --- /dev/null +++ b/libraries/TrueRandom/examples/Magic8Ball/Magic8Ball.ino @@ -0,0 +1,59 @@ +/* + * A magic 8 ball. + * + * Press the reset button to see into the future. + * + * View the answer to your question in the Serial Monitor, at 19200 baud. + * + * Press the Arduino reset button to ask another question. + * + */ + +#include + +char* answers[20] = { + "As I see it, yes", + "It is certain", + "It is decidedly so", + "Mostly likely", + "Outlook good", + "Signs point to yes", + "Without a doubt", + "Yes", + "Yes - definitely", + "You may rely on it", + "Reply hazy, try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again", + "Don't count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful" +}; + +int answerNumber; + +void setup() { + Serial.begin(9600); + + Serial.print("The answer is "); + + // Dramatic pause + delay(1000); + Serial.print(". "); + delay(1000); + Serial.print(". "); + delay(1000); + Serial.print(". "); + delay(1000); + + answerNumber = TrueRandom.random(20); + Serial.println( answers[answerNumber] ); +} + +void loop() { + ; // Do nothing +} \ No newline at end of file diff --git a/libraries/TrueRandom/examples/SetRandomSeed/SetRandomSeed.ino b/libraries/TrueRandom/examples/SetRandomSeed/SetRandomSeed.ino new file mode 100644 index 00000000..e9f009ce --- /dev/null +++ b/libraries/TrueRandom/examples/SetRandomSeed/SetRandomSeed.ino @@ -0,0 +1,28 @@ +/* + * SetRandomSeed. + * + * You can use TrueRandom to set the seed for the normal Arduino + * random number generator. + * + * That way you can quickly generate random numbers that are + * different every time using the random number generator. + */ + +#include + +int i; + +void setup() { + Serial.begin(9600); + Serial.println("Here are some pseudo random digits."); + for (i=1;i<=20;i++) Serial.print(random(10)); + Serial.println(); + + randomSeed(TrueRandom.random()); + + Serial.println("Here are some random seeded pseudo random digits."); + for (i=1;i<=20;i++) Serial.print(random(10)); + Serial.println(); +} +void loop() { +} \ No newline at end of file diff --git a/libraries/TrueRandom/examples/Uuid/Uuid.ino b/libraries/TrueRandom/examples/Uuid/Uuid.ino new file mode 100644 index 00000000..3f80db3d --- /dev/null +++ b/libraries/TrueRandom/examples/Uuid/Uuid.ino @@ -0,0 +1,58 @@ +/* + * Uuid + * + * UUIDs are unique numbers that are used for identifying individual units, + * functions, programmes, or whatever you want to tag. + * + * In this demo, press the Arduino Reset button to generate a new number. + * + * UUIDs can be assigned sequentially from allocated blocks of numbers, but + * they are most powerful when randomly assigned. UUIDs are such big numbers + * that, for all effective purposes, no two numbers will ever match. + * + * UUIDs are particularly useful in web-aware devices, or radio networks. + * + * For a discussion of the use of UUIDs, see + * http://en.wikipedia.org/wiki/Universally_Unique_Identifier + * + * For implementation details of UUIDs, see + * http://tools.ietf.org/html/rfc4122 + */ + +#include + +byte uuidNumber[16]; // UUIDs in binary form are 16 bytes long + +void printHex(byte number) { + int topDigit = number >> 4; + int bottomDigit = number & 0x0f; + // Print high hex digit + Serial.print( "0123456789ABCDEF"[topDigit] ); + // Low hex digit + Serial.print( "0123456789ABCDEF"[bottomDigit] ); +} + +void printUuid(byte* uuidNumber) { + int i; + for (i=0; i<16; i++) { + if (i==4) Serial.print("-"); + if (i==6) Serial.print("-"); + if (i==8) Serial.print("-"); + if (i==10) Serial.print("-"); + printHex(uuidNumber[i]); + } +} + +void setup() { + Serial.begin(9600); + + // Generate a new UUID + TrueRandom.uuid(uuidNumber); + + Serial.print("The UUID number is "); + printUuid(uuidNumber); + Serial.println(); +} + +void loop() { +} \ No newline at end of file diff --git a/libraries/TrueRandom/keywords.txt b/libraries/TrueRandom/keywords.txt new file mode 100644 index 00000000..07504d75 --- /dev/null +++ b/libraries/TrueRandom/keywords.txt @@ -0,0 +1,26 @@ +####################################### +# Syntax Coloring Map For TrueRandom +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +TrueRandom KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +rand KEYWORD2 +random KEYWORD2 +randomBit KEYWORD2 +randomByte KEYWORD2 +memfill KEYWORD2 +mac KEYWORD2 +uuid KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/TrueRandom/library.properties b/libraries/TrueRandom/library.properties new file mode 100644 index 00000000..407ea5a2 --- /dev/null +++ b/libraries/TrueRandom/library.properties @@ -0,0 +1,9 @@ +name=TrueRandom +version=1.2 +author= +maintainer= +sentence=Hardware bases random numbers. +paragraph=Generates random numbers using analog pin(avr) or hardware random number generator(nRF5). Compatible to https://github.com/sirleech/TrueRandom +category=Other +url= +architectures=avr,nRF5 diff --git a/libraries/TrueRandom/release notes.txt b/libraries/TrueRandom/release notes.txt new file mode 100644 index 00000000..72410e18 --- /dev/null +++ b/libraries/TrueRandom/release notes.txt @@ -0,0 +1,7 @@ +TrueRandom library +by Peter Knight, Tinker.it! 2010 +http://code.google.com/p/tinkerit + +v1: First release +v1.1: Bug fix for rand and random functions +v1.2: Port to nordic nRF5