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: */


//32 guaranteed random numbers
const uint8_t random0[] PROGMEM = {
  0x20, 0x37, 0xF6, 0xD9, 0x6F, 0x8D, 0x9F, 0xE6, 0x9D, 0x83, 0x15, 0xDC, 0xCB, 0xEB, 0x6F, 0x40, 0x62, 0x25, 0x14, 0xF1, 0x70, 0x58, 0xC9, 0x25, 0x1A, 0x1A, 0xF4, 0x2F, 0xCB, 0x62, 0xCC, 0xEC, 

#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 *