Online random number generator for AVR microcontroller

This is an online random number generator for an AVR microcontroller, you can use it to include some randomness in your project.

guaranteed random numbers
When using these static random numbers in more than one AVR controller, they all got the same random numbers, however all oscillators differ somewhat in speed over time and thus multiple AVR controllers using the same random number table do really seem random. The effect is the biggest when using the internal RC oscillator because from AVR to AVR the speed differs the most.

How many random numbers do you need? Hint: you can fill the rest of the free program flash memory with random numbers.

Save this as “randomvals.h”:

/* generated using: http://atoomnet.net/?p=462 */

#ifndef RANDOMVALS_H_
#define RANDOMVALS_H_

//32 guaranteed random numbers
const uint8_t random0[] PROGMEM = {
  0xDF, 0x04, 0xC3, 0x34, 0xF6, 0xD7, 0x39, 0x49, 0xCC, 0xB2, 0x5E, 0x5F, 0x87, 0xF8, 0x2D, 0x15, 0x23, 0xF1, 0x78, 0xA7, 0x69, 0x24, 0xE7, 0x7F, 0xFD, 0xC3, 0xB2, 0xE5, 0x1F, 0x33, 0xE0, 0xAD, 
};

#endif /* RANDOMVALS_H_ */

Save this as “random.h”:

#ifndef RANDOM_H_
#define RANDOM_H_

uint8_t READrnd(void);

#endif /* RANDOM_H_ */

Save this as “random.c”

#include <avr/pgmspace.h>
#include "randomvals.h"

uint16_t ptrRnd = 0;

//Read a random value from randomvals.h, wrap when reaching the end of the table.
uint8_t READrnd()
{
  uint8_t rnd = pgm_read_byte(&(random0[ptrRnd++]));

  if (ptrRnd==sizeof(random0))
    ptrRnd = 0;

  return rnd;
}

Move the saved files into your project.

Example usage:

#include "random.h"

int main(void)
{
  //Get a random value
  uint8_t rnd = READrnd();
}

Did you like this post? Please give it a thumbs up by sharing it on your social network. Thanks!

One thought on “Online random number generator for AVR microcontroller

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>