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 = {
  0x7C, 0x89, 0x11, 0x88, 0x55, 0x96, 0x04, 0xDD, 0x99, 0x86, 0x1C, 0x0F, 0xE8, 0xCA, 0xD1, 0x9C, 0x92, 0x52, 0xDD, 0x56, 0x0D, 0x62, 0x55, 0x0A, 0xEC, 0x46, 0xD8, 0xC9, 0xCC, 0xB5, 0xB1, 0xC9, 

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

3 replies on “Online random number generator for AVR microcontroller”

Leave a Reply

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