Online random number generator for MSP430 microcontroller

This is an online random number generator for a MSP430 microcontroller, you can use it to include some randomness in your project.
When using these static random numbers in more than one MSP430 controller, they all got the same random numbers, however all oscillators differ somewhat in speed over time and thus multiple MSP430 controllers using the same random number table do really seem random.

How many random numbers do you need? Hint: you can fill the rest of the free program flash memory with random numbers. (Open the .map file, scroll to ‘MEMORY CONFIGURATION’ and lookup the ‘unused’ column on the ‘FLASH’ row.)

Save this as “randomvals.h”:

/* generated using: */


//32 guaranteed random numbers
const uint8_t random0[] PROGMEM = {
  0xF7, 0x01, 0x1F, 0xA2, 0x4B, 0xD6, 0xD9, 0x2D, 0xD4, 0x81, 0x3C, 0xEB, 0xCE, 0x16, 0x9E, 0x10, 0xD5, 0x4C, 0x91, 0xC2, 0xCF, 0xD7, 0x03, 0x1F, 0xA9, 0x18, 0x5D, 0x88, 0x89, 0x88, 0xA8, 0xB4, 

#endif /* RANDOMVALS_H_ */

Save this as “random.h”:

#ifndef RANDOM_H_
#define RANDOM_H_

#include <stdint.h>

uint8_t READrnd(void);

#endif /* RANDOM_H_ */

Save this as “random.c”

#include <stdint.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 = random0[ptrRnd++];

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

  return rnd;

Move the saved files into your project.

This example outputs a 8-bit random value to PORT1 of the MSP430 microcontroller.
When run on the TI Launchpad, the LED1 and LED2 will flash randomly. The example was tested on the M430G2553, MSP430G2452, MSP430G2353 and the MSP430G2221 microcontroller.

#include <msp430.h>
#include <stdint.h>

#include "random.h"

int main(void)
  int i;
  WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

  P1DIR = 0xFF; //Port 1 all pins output

    uint8_t randomValue = READrnd(); //Get 8-bit random value from array

    P1OUT = randomValue; //Output random value to port 1

    //Do nothing for some time
    for(i = 10000; i >= 0; i--)


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

Leave a Reply

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