Generate true random numbers on microcontroller

Sometimes there is really a problem of how to generate true random numbers using your microcontroller. Usually computer processor or any other MCU is capable to generate a Pseudo Random Number (PRN). These numbers are generated by algorithms so called Pseudo Random Number Generators (PRNG). Everything what pure algorithm produces is predictable in some sort of level.

 

There are many PRNG algorithms that generate random numbers, but there is always a defined number of iterations when random number sequence will repeat itself. Sometimes it may be acceptable. One popular way to generate pseudo random numbers is using Timers. Moreuniversal algorithm is concept of Linear Feedback Shift Register (LFSR). LSFR is a n -bit register which is initiated with non zero seed value and is clocked by shifting values to the left and loading new bit in to bit0. New bit is calculated by XOR’ing the bits of selected taps of LSFR. This method is used in rand() functions.

Usually we know simple solution of random number generation (AVR-GCC example):

//Example how to generate PRN in range (0 to 9)

uint8_t randNumber;
// Get a random number (0 to 255)
randNumber = (uint8_t) rand();

// Set number range to 0 to 15
randNumber = randNumber & 0x0F;
// Set number range to 0 to 9
if (randNumber > 9)
randNumber -= 6;

But this algorithm will always get the same numerical order as long the same seed for the rand() function is used. This is nothing more than mathematical function that cycles through a range of numbers which can be predictable. If you need really true randomness you need to find real world source that could inject some entropy. This could be any noisy diode connected to ADC. Such ramdom generators are so called Hardware Random Number Generators. They often use some microscopic phenomena like thermal noise, photoelectric effect, etc. There are complete random number generators in the market, that can be connected to PC via USB like this http://random.com.hr/products/hg400/index.html.

There is interesting reading about Random Noise Sources where as entropy source is Zener diode used. Measurements are done using PC Sound Card.

In general if you are prototyping some sort of Embedded platform with temperature sensor like AD7416 this menas that you already have a hardware random number generator. Because temperature sensor chip’s generates noise which can be used as source of entropy for your RNG. And you don’t need to connect additional devices like Zener diodes or photo cells.

 

 
FreeBSD/NetBSD/Linux OS Cross Reference
ROOT
/lib/libc/stdlib/rand.c
1 /* $NetBSD: rand.c,v 1.10 2003/08/07 16:43:43 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30
*/
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93";
36 #else
37 __RCSID("$NetBSD: rand.c,v 1.10 2003/08/07 16:43:43 agc Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/types.h>
42 #include <stdlib.h>
43
44 static u_long next = 1;
45
46 int
47 rand()
48 {
49 /* LINTED integer overflow */
50 return (int)((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1));
51 }
52
53 void
54 srand(seed)
55 u_int seed;
56 {
57 next = seed;
58 }
59

 

 
/* rand.c - rand, srand functions for stdlib */



/* Copyright 1992-1993 Wind River Systems, Inc. */



/*

modification history

--------------------

01c,08feb93,jdi documentation cleanup for 5.1.

01b,20sep92,smb documentation additions.

01a,19jul92,smb written and documented.

*/



/*

DESCRIPTION



INCLUDE FILES: stdlib.h



SEE ALSO: American National Standard X3.159-1989



NOMANUAL

*/



#include
"vxWorks.h"

#include
"stdlib.h"



ulong_t _Randseed
= 1;



/*******************************************************************************

*

* rand - generate a pseudo-random integer between 0 and RAND_MAX (ANSI)

*

* This routine generates a pseudo-random integer between 0 and RAND_MAX.

* The seed value for rand() can be reset with srand().

*

* INCLUDE FILES: stdlib.h

*

* RETURNS: A pseudo-random integer.

*

* SEE ALSO: srand()

*/



int rand (void)

{

_Randseed
= _Randseed * 1103515245 + 12345;

return (uint_t) (_Randseed/65536) % (RAND_MAX + 1);

}



/*******************************************************************************

*

* srand - reset the value of the seed used to generate random numbers (ANSI)

*

* This routine resets the seed value used by rand(). If srand() is then

* called with the same seed value, the sequence of pseudo-random numbers is

* repeated. If rand() is called before any calls to srand() have been made,

* the same sequence shall be generated as when srand() is first called with

* the seed value of 1.

*

* INCLUDE FILES: stdlib.h

*

* RETURNS: N/A

*

* SEE ALSO: rand()

*/



void * srand

(

uint_t seed
/* random number seed */

)

{

_Randseed
= seed;

return (void *)0;

}

 

posted on 2011-02-03 14:30  cnmaizi  阅读(954)  评论(0编辑  收藏  举报