├── .DS_Store ├── examples ├── .DS_Store ├── SetRandomSeed │ └── SetRandomSeed.pde ├── Die │ └── Die.pde ├── Benchmark │ └── Benchmark.pde ├── Magic8Ball │ └── Magic8Ball.pde ├── MacAddress │ └── MacAddress.pde ├── Uuid │ └── Uuid.pde └── AllFunctions │ └── AllFunctions.pde ├── release notes.txt ├── keywords.txt ├── TrueRandom.h ├── readme └── TrueRandom.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sirleech/TrueRandom/HEAD/.DS_Store -------------------------------------------------------------------------------- /examples/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sirleech/TrueRandom/HEAD/examples/.DS_Store -------------------------------------------------------------------------------- /release notes.txt: -------------------------------------------------------------------------------- 1 | TrueRandom library 2 | by Peter Knight, Tinker.it! 2010 3 | http://code.google.com/p/tinkerit 4 | 5 | v1: First release 6 | v1.1: Bug fix for rand and random functions -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For TrueRandom 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | TrueRandom KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | rand KEYWORD2 16 | random KEYWORD2 17 | randomBit KEYWORD2 18 | randomByte KEYWORD2 19 | memfill KEYWORD2 20 | mac KEYWORD2 21 | uuid KEYWORD2 22 | 23 | ####################################### 24 | # Constants (LITERAL1) 25 | ####################################### 26 | 27 | -------------------------------------------------------------------------------- /TrueRandom.h: -------------------------------------------------------------------------------- 1 | /** 2 | * TrueRandom - A true random number generator for Arduino. 3 | * 4 | * Copyright (c) 2010 Peter Knight, Tinker.it! All rights reserved. 5 | */ 6 | 7 | #ifndef TrueRandom_h 8 | #define TrueRandom_h 9 | 10 | #include 11 | class TrueRandomClass 12 | { 13 | public: 14 | int rand(); 15 | long random(); 16 | long random(long howBig); 17 | long random(long howsmall, long how); 18 | int randomBit(void); 19 | char randomByte(void); 20 | void memfill(char* location, int size); 21 | void mac(uint8_t* macLocation); 22 | void uuid(uint8_t* uuidLocation); 23 | private: 24 | int randomBitRaw(void); 25 | int randomBitRaw2(void); 26 | }; 27 | extern TrueRandomClass TrueRandom; 28 | #endif -------------------------------------------------------------------------------- /examples/SetRandomSeed/SetRandomSeed.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * SetRandomSeed. 3 | * 4 | * You can use TrueRandom to set the seed for the normal Arduino 5 | * random number generator. 6 | * 7 | * That way you can quickly generate random numbers that are 8 | * different every time using the random number generator. 9 | */ 10 | 11 | #include 12 | 13 | int i; 14 | 15 | void setup() { 16 | Serial.begin(9600); 17 | Serial.println("Here are some pseudo random digits."); 18 | for (i=1;i<=20;i++) Serial.print(random(10)); 19 | Serial.println(); 20 | 21 | randomSeed(TrueRandom.random()); 22 | 23 | Serial.println("Here are some random seeded pseudo random digits."); 24 | for (i=1;i<=20;i++) Serial.print(random(10)); 25 | Serial.println(); 26 | } 27 | void loop() { 28 | } -------------------------------------------------------------------------------- /examples/Die/Die.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * A simple electronic die. 3 | * 4 | * Press the reset button to throw a set of dice. 5 | * 6 | */ 7 | 8 | #include 9 | 10 | void setup() { 11 | Serial.begin(9600); 12 | Serial.println("Throwing..."); 13 | delay(1000); 14 | 15 | Serial.print("6 sided die: "); 16 | Serial.println(TrueRandom.random(1,7)); 17 | Serial.print("4 sided die: "); 18 | Serial.println(TrueRandom.random(1,5)); 19 | Serial.print("8 sided die: "); 20 | Serial.println(TrueRandom.random(1,9)); 21 | Serial.print("10 sided die: "); 22 | Serial.println(TrueRandom.random(1,11)); 23 | Serial.print("12 sided die: "); 24 | Serial.println(TrueRandom.random(1,13)); 25 | Serial.print("20 sided die: "); 26 | Serial.println(TrueRandom.random(1,21)); 27 | Serial.print("100 sided die: "); 28 | Serial.println(TrueRandom.random(1,101)); 29 | } 30 | 31 | void loop() { 32 | ; // Do nothing 33 | } -------------------------------------------------------------------------------- /examples/Benchmark/Benchmark.pde: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned long startTime; 4 | int i; 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | 9 | Serial.println("TrueRandom benchmark"); 10 | Serial.println("--------------------"); 11 | Serial.println(); 12 | 13 | Serial.print("Arduino clock speed: "); 14 | Serial.print(F_CPU/1000000); 15 | Serial.println("MHz"); 16 | 17 | Serial.print("randomBit(): "); 18 | startTime = millis(); 19 | TrueRandom.randomBit(); 20 | Serial.print(millis() - startTime); 21 | Serial.println("ms"); 22 | 23 | Serial.print("randomByte(): "); 24 | startTime = millis(); 25 | TrueRandom.randomByte(); 26 | Serial.print(millis() - startTime); 27 | Serial.println("ms"); 28 | 29 | Serial.print("random(100): "); 30 | startTime = millis(); 31 | TrueRandom.random(100); 32 | Serial.print(millis() - startTime); 33 | Serial.println("ms"); 34 | 35 | Serial.print("random(65536): "); 36 | startTime = millis(); 37 | TrueRandom.random(65536); 38 | Serial.print(millis() - startTime); 39 | Serial.println("ms"); 40 | 41 | Serial.print("random(65537): "); 42 | startTime = millis(); 43 | TrueRandom.random(65537); 44 | Serial.print(millis() - startTime); 45 | Serial.println("ms"); 46 | } 47 | void loop() { 48 | } -------------------------------------------------------------------------------- /examples/Magic8Ball/Magic8Ball.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * A magic 8 ball. 3 | * 4 | * Press the reset button to see into the future. 5 | * 6 | * View the answer to your question in the Serial Monitor, at 19200 baud. 7 | * 8 | * Press the Arduino reset button to ask another question. 9 | * 10 | */ 11 | 12 | #include 13 | 14 | char* answers[20] = { 15 | "As I see it, yes", 16 | "It is certain", 17 | "It is decidedly so", 18 | "Mostly likely", 19 | "Outlook good", 20 | "Signs point to yes", 21 | "Without a doubt", 22 | "Yes", 23 | "Yes - definitely", 24 | "You may rely on it", 25 | "Reply hazy, try again", 26 | "Ask again later", 27 | "Better not tell you now", 28 | "Cannot predict now", 29 | "Concentrate and ask again", 30 | "Don't count on it", 31 | "My reply is no", 32 | "My sources say no", 33 | "Outlook not so good", 34 | "Very doubtful" 35 | }; 36 | 37 | int answerNumber; 38 | 39 | void setup() { 40 | Serial.begin(9600); 41 | 42 | Serial.print("The answer is "); 43 | 44 | // Dramatic pause 45 | delay(1000); 46 | Serial.print(". "); 47 | delay(1000); 48 | Serial.print(". "); 49 | delay(1000); 50 | Serial.print(". "); 51 | delay(1000); 52 | 53 | answerNumber = TrueRandom.random(20); 54 | Serial.println( answers[answerNumber] ); 55 | } 56 | 57 | void loop() { 58 | ; // Do nothing 59 | } -------------------------------------------------------------------------------- /examples/MacAddress/MacAddress.pde: -------------------------------------------------------------------------------- 1 | // This example modifies the Ethernet Client example to use 2 | // a randomly generated MAC address 3 | // 4 | // This has the advantage that no MAC addresses will clash, even if 5 | // every device uses exactly the same code on its Arduino. 6 | // 7 | 8 | // This example is identical to the Ethernet/Client example 9 | // with three changes to utilise TrueRandom. 10 | 11 | #include 12 | 13 | // 14 | // Change 1/3: Include TrueRandom library 15 | #include 16 | // 17 | // 18 | 19 | // 20 | // Change 2/3: Include TrueRandom library 21 | // TrueRandom sets this, so no default is needed here. 22 | byte mac[6]; 23 | // 24 | // 25 | 26 | byte ip[] = { 10, 0, 0, 177 }; 27 | byte server[] = { 64, 233, 187, 99 }; // Google 28 | 29 | Client client(server, 80); 30 | 31 | void setup() 32 | { 33 | // 34 | // Change 3/3: Set a random MAC address using TrueRandom 35 | TrueRandom.mac(mac); 36 | // 37 | // 38 | 39 | 40 | Ethernet.begin(mac, ip); 41 | Serial.begin(9600); 42 | 43 | delay(1000); 44 | 45 | Serial.println("connecting..."); 46 | 47 | if (client.connect()) { 48 | Serial.println("connected"); 49 | client.println("GET /search?q=arduino HTTP/1.0"); 50 | client.println(); 51 | } else { 52 | Serial.println("connection failed"); 53 | } 54 | } 55 | 56 | void loop() 57 | { 58 | if (client.available()) { 59 | char c = client.read(); 60 | Serial.print(c); 61 | } 62 | 63 | if (!client.connected()) { 64 | Serial.println(); 65 | Serial.println("disconnecting."); 66 | client.stop(); 67 | for(;;) 68 | ; 69 | } 70 | } -------------------------------------------------------------------------------- /examples/Uuid/Uuid.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * Uuid 3 | * 4 | * UUIDs are unique numbers that are used for identifying individual units, 5 | * functions, programmes, or whatever you want to tag. 6 | * 7 | * In this demo, press the Arduino Reset button to generate a new number. 8 | * 9 | * UUIDs can be assigned sequentially from allocated blocks of numbers, but 10 | * they are most powerful when randomly assigned. UUIDs are such big numbers 11 | * that, for all effective purposes, no two numbers will ever match. 12 | * 13 | * UUIDs are particularly useful in web-aware devices, or radio networks. 14 | * 15 | * For a discussion of the use of UUIDs, see 16 | * http://en.wikipedia.org/wiki/Universally_Unique_Identifier 17 | * 18 | * For implementation details of UUIDs, see 19 | * http://tools.ietf.org/html/rfc4122 20 | */ 21 | 22 | #include 23 | 24 | byte uuidNumber[16]; // UUIDs in binary form are 16 bytes long 25 | 26 | void printHex(byte number) { 27 | int topDigit = number >> 4; 28 | int bottomDigit = number & 0x0f; 29 | // Print high hex digit 30 | Serial.print( "0123456789ABCDEF"[topDigit] ); 31 | // Low hex digit 32 | Serial.print( "0123456789ABCDEF"[bottomDigit] ); 33 | } 34 | 35 | void printUuid(byte* uuidNumber) { 36 | int i; 37 | for (i=0; i<16; i++) { 38 | if (i==4) Serial.print("-"); 39 | if (i==6) Serial.print("-"); 40 | if (i==8) Serial.print("-"); 41 | if (i==10) Serial.print("-"); 42 | printHex(uuidNumber[i]); 43 | } 44 | } 45 | 46 | void setup() { 47 | Serial.begin(9600); 48 | 49 | // Generate a new UUID 50 | TrueRandom.uuid(uuidNumber); 51 | 52 | Serial.print("The UUID number is "); 53 | printUuid(uuidNumber); 54 | Serial.println(); 55 | } 56 | 57 | void loop() { 58 | } -------------------------------------------------------------------------------- /examples/AllFunctions/AllFunctions.pde: -------------------------------------------------------------------------------- 1 | /* 2 | * A magic 8 ball. 3 | * 4 | * Press the reset button to see into the future. 5 | * 6 | * View the answer to your question in the Serial Monitor, at 19200 baud. 7 | * 8 | * Press the Arduino reset button to ask another question. 9 | * 10 | */ 11 | 12 | #include 13 | 14 | char array[10]; 15 | int arrayLength = 10; 16 | 17 | char macAddress[6]; // MAC addresses are always 6 bytes long 18 | char uuidNumber[16]; // UUIDs are always 16 bytes long 19 | 20 | void printHex(char number) { 21 | // Print high hex digit 22 | Serial.print( "0123456789ABCDEF"[number / 16] ); 23 | // Low hex digit 24 | Serial.print( "0123456789ABCDEF"[number & 15] ); 25 | } 26 | 27 | void printMac(char* macAddress) { 28 | // Print a MAC address in the form 29 | // 12:23:34:45:56:67 30 | int i; 31 | for (i=0; i<6; i++) { 32 | printHex(macAddress[i]); 33 | if (i<5) Serial.print(":"); 34 | } 35 | } 36 | void printUuid(char* uuidNumber) { 37 | // Print a UUID in the form 38 | // 12345678-1234-1234-1234-123456789ABC 39 | int i; 40 | for (i=0; i<16; i++) { 41 | if (i==4) Serial.print("-"); 42 | if (i==6) Serial.print("-"); 43 | if (i==8) Serial.print("-"); 44 | if (i==10) Serial.print("-"); 45 | printHex(uuid[i]); 46 | } 47 | } 48 | 49 | void setup() { 50 | Serial.begin(9600); 51 | 52 | // TrueRandom.rand() returns a positive integer 53 | // in the range 0..32767 54 | 55 | Serial.print("A random int: "); 56 | Serial.println(TrueRandom.rand()); 57 | 58 | // TrueRandom.random() returns a positive long integer 59 | // in the range 0..2,147,483,647 60 | 61 | Serial.print("A random long int: "); 62 | Serial.println(TrueRandom.random()); 63 | 64 | // TrueRandom.random(n) returns a positive long integer 65 | // in the range 0 .. (n-1) 66 | 67 | Serial.print("A random number between 0 and 999: "); 68 | Serial.println(TrueRandom.random(1000)); 69 | 70 | // TrueRandom.random(a,b) returns a positive long integer 71 | // in the range a .. (b-1). 72 | // b must be larger than a. 73 | 74 | Serial.print("A random number between 1000 and 9999: "); 75 | Serial.println(TrueRandom.random(1000,10000)); 76 | 77 | // TrueRandom.randomBit() returns a single random bit 78 | // It returns either 1 or 0. 79 | Serial.print("A random bit: "); 80 | Serial.println(TrueRandom.randomBit()); 81 | 82 | // TrueRandom.randomByte() returns a single random byte 83 | // It returns an 8 bit char (byte) in the range -128 to 127. 84 | Serial.print("A random byte: "); 85 | Serial.println(TrueRandom.randomByte(),DEC); 86 | 87 | // Zero an array 88 | for(i=0; i 18 | 19 | void setup() { 20 | Serial.begin(9600); 21 | 22 | Serial.print("I threw a random die and got "); 23 | Serial.print(random(1,7)); 24 | 25 | Serial.print(". Then I threw a TrueRandom die and got "); 26 | Serial.println(TrueRandom.random(1,7)); 27 | 28 | } 29 | 30 | void loop() { 31 | ; // Do nothing 32 | } 33 | Upload that code to an Arduino Duemilanove 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. 34 | 35 | TrueRandom basic functions 36 | The existing random functions of Arduino are replicated in TrueRandom. 37 | 38 | TrueRandom.random() 39 | Like the Arduino library and ANSI C, this generates a random number between 0 and the highest signed long integer 2,147,483,647. 40 | 41 | TrueRandom.random(n) 42 | This generates a random number between 0 and (n-1). So random(6) will generate numbers between 0 and 5. 43 | 44 | TrueRandom.random(a,b) 45 | This generates a random number between a and (b-1). So random(1,7) will generate numbers between 1 and 6. 46 | 47 | TrueRandom advanced functions 48 | TrueRandom.randomBit() 49 | 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. 50 | 51 | TrueRandom.randomByte() 52 | Generates a random byte between 0 and 255. Equivalent to random(256). 53 | 54 | TrueRandom.rand() 55 | Like the ANSI C rand() command, this generates a random number between 0 and the highest signed integer 32767. 56 | 57 | TrueRandom.memfill(address, length) 58 | Fills a block of bytes with random numbers. (length) bytes are filled in total, starting at the given (address). 59 | 60 | TrueRandom specialist functions 61 | TrueRandom.mac(address) 62 | 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. 63 | 64 | TrueRandom.uuid(address) 65 | 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. 66 | 67 | How TrueRandom works 68 | 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. 69 | 70 | 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. 71 | 72 | Projects using TrueRandom 73 | Generative Music from Gijs -------------------------------------------------------------------------------- /TrueRandom.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TrueRandom - A true random number generator for Arduino. 3 | * 4 | * Copyright (c) 2010 Peter Knight, Tinker.it! All rights reserved. 5 | */ 6 | 7 | #include 8 | #include "TrueRandom.h" 9 | 10 | int TrueRandomClass::randomBitRaw(void) { 11 | uint8_t copyAdmux, copyAdcsra, copyAdcsrb, copyPortc, copyDdrc; 12 | uint16_t i; 13 | uint8_t bit; 14 | volatile uint8_t dummy; 15 | 16 | // Store all the registers we'll be playing with 17 | copyAdmux = ADMUX; 18 | copyAdcsra = ADCSRA; 19 | copyAdcsrb = ADCSRB; 20 | copyPortc = PORTC; 21 | copyDdrc = DDRC; 22 | 23 | // Perform a conversion on Analog0, using the Vcc reference 24 | ADMUX = _BV(REFS0); 25 | 26 | #if F_CPU > 16000000 27 | // ADC is enabled, divide by 32 prescaler 28 | ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS0); 29 | #elif F_CPU > 8000000 30 | // ADC is enabled, divide by 16 prescaler 31 | ADCSRA = _BV(ADEN) | _BV(ADPS2); 32 | #else 33 | // ADC is enabled, divide by 8 prescaler 34 | ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); 35 | #endif 36 | 37 | // Autotriggering disabled 38 | ADCSRB = 0; 39 | 40 | // Pull Analog0 to ground 41 | PORTC &=~_BV(0); 42 | DDRC |= _BV(0); 43 | // Release Analog0, apply internal pullup 44 | DDRC &= ~_BV(0); 45 | PORTC |= _BV(1); 46 | // Immediately start a sample conversion on Analog0 47 | ADCSRA |= _BV(ADSC); 48 | // Wait for conversion to complete 49 | while (ADCSRA & _BV(ADSC)) PORTC ^= _BV(0); 50 | // Xor least significant bits together 51 | bit = ADCL; 52 | // We're ignoring the high bits, but we have to read them before the next conversion 53 | dummy = ADCH; 54 | 55 | // Restore register states 56 | ADMUX = copyAdmux; 57 | ADCSRA = copyAdcsra; 58 | ADCSRB = copyAdcsrb; 59 | PORTC = copyPortc; 60 | DDRC = copyDdrc; 61 | 62 | return bit & 1; 63 | } 64 | 65 | int TrueRandomClass::randomBitRaw2(void) { 66 | // Software whiten bits using Von Neumann algorithm 67 | // 68 | // von Neumann, John (1951). "Various techniques used in connection 69 | // with random digits". National Bureau of Standards Applied Math Series 70 | // 12:36. 71 | // 72 | for(;;) { 73 | int a = randomBitRaw() | (randomBitRaw()<<1); 74 | if (a==1) return 0; // 1 to 0 transition: log a zero bit 75 | if (a==2) return 1; // 0 to 1 transition: log a one bit 76 | // For other cases, try again. 77 | } 78 | } 79 | 80 | int TrueRandomClass::randomBit(void) { 81 | // Software whiten bits using Von Neumann algorithm 82 | // 83 | // von Neumann, John (1951). "Various techniques used in connection 84 | // with random digits". National Bureau of Standards Applied Math Series 85 | // 12:36. 86 | // 87 | for(;;) { 88 | int a = randomBitRaw2() | (randomBitRaw2()<<1); 89 | if (a==1) return 0; // 1 to 0 transition: log a zero bit 90 | if (a==2) return 1; // 0 to 1 transition: log a one bit 91 | // For other cases, try again. 92 | } 93 | } 94 | 95 | char TrueRandomClass::randomByte(void) { 96 | char result; 97 | uint8_t i; 98 | result = 0; 99 | for (i=8; i--;) result += result + randomBit(); 100 | return result; 101 | } 102 | 103 | int TrueRandomClass::rand() { 104 | int result; 105 | uint8_t i; 106 | result = 0; 107 | for (i=15; i--;) result += result + randomBit(); 108 | return result; 109 | } 110 | 111 | long TrueRandomClass::random() { 112 | long result; 113 | uint8_t i; 114 | result = 0; 115 | for (i=31; i--;) result += result + randomBit(); 116 | return result; 117 | } 118 | 119 | long TrueRandomClass::random(long howBig) { 120 | long randomValue; 121 | long maxRandomValue; 122 | long topBit; 123 | long bitPosition; 124 | 125 | if (!howBig) return 0; 126 | randomValue = 0; 127 | if (howBig & (howBig-1)) { 128 | // Range is not a power of 2 - use slow method 129 | topBit = howBig-1; 130 | topBit |= topBit>>1; 131 | topBit |= topBit>>2; 132 | topBit |= topBit>>4; 133 | topBit |= topBit>>8; 134 | topBit |= topBit>>16; 135 | topBit = (topBit+1) >> 1; 136 | 137 | bitPosition = topBit; 138 | do { 139 | // Generate the next bit of the result 140 | if (randomBit()) randomValue |= bitPosition; 141 | 142 | // Check if bit 143 | if (randomValue >= howBig) { 144 | // Number is over the top limit - start again. 145 | randomValue = 0; 146 | bitPosition = topBit; 147 | } else { 148 | // Repeat for next bit 149 | bitPosition >>= 1; 150 | } 151 | } while (bitPosition); 152 | } else { 153 | // Special case, howBig is a power of 2 154 | bitPosition = howBig >> 1; 155 | while (bitPosition) { 156 | if (randomBit()) randomValue |= bitPosition; 157 | bitPosition >>= 1; 158 | } 159 | } 160 | return randomValue; 161 | } 162 | 163 | long TrueRandomClass::random(long howSmall, long howBig) { 164 | if (howSmall >= howBig) return howSmall; 165 | long diff = howBig - howSmall; 166 | return TrueRandomClass::random(diff) + howSmall; 167 | } 168 | 169 | void TrueRandomClass::memfill(char* location, int size) { 170 | for (;size--;) *location++ = randomByte(); 171 | } 172 | 173 | void TrueRandomClass::mac(uint8_t* macLocation) { 174 | memfill((char*)macLocation,6); 175 | } 176 | 177 | void TrueRandomClass::uuid(uint8_t* uuidLocation) { 178 | // Generate a Version 4 UUID according to RFC4122 179 | memfill((char*)uuidLocation,16); 180 | // Although the UUID contains 128 bits, only 122 of those are random. 181 | // The other 6 bits are fixed, to indicate a version number. 182 | uuidLocation[6] = 0x40 | (0x0F & uuidLocation[6]); 183 | uuidLocation[8] = 0x80 | (0x3F & uuidLocation[8]); 184 | } 185 | 186 | TrueRandomClass TrueRandom; 187 | --------------------------------------------------------------------------------