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 = {
  0x3A, 0xF9, 0x37, 0xE4, 0xFB, 0x40, 0x00, 0xF6, 0x79, 0xD7, 0xB7, 0x0E, 0x71, 0xF7, 0xC0, 0x78, 0x87, 0x9E, 0xCF, 0xEA, 0x34, 0xCE, 0x6E, 0x0A, 0x55, 0x18, 0x74, 0x13, 0x95, 0xA7, 0xD7, 0xCC, 

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