Random and pseudorandom number generation for the MSP430, implemented in C.


  1. make rand
  2. #include "msp430-rng/rand.h"
  3. msp430-elf-gcc -o foo.elf foo.o rand.o


Random (rand())

Truly random number generator. Uses the time difference between the VLO and DCO, as outlined in SLAA338. It is self-contained, but it mucks about with clocks and timers, so it is only intended to be used very early on to generate the seed for a PRNG.

Pseudorandom (prand())

A very simple and fast PRNG implemented by a linear congruential generator with the following values:

  • modulus: 65536 (2^16)
  • multiplier: 49381
  • increment: 8643

Supported compilers

Currently only tested using the gcc msp430-elf cross-compiler (not mspgcc). Patches with modifications for other environments are welcome.


To run the tests, make test and run test.elf on your MSP430. They should take less than a minute to finish.

The test mechanism assumes that you are using a Launchpad, with LEDs on pins 0 and 6. While the tests are running, both LEDs are on; when the tests finish, one of the LEDs blinks (green: pass, red: fail).

Using mspdebug, it is easy to see which test is failing and possibly why:

prog test.elf
md status 2
md sum_rand 2
md sum_prand 2


An implementation of the Monobit algorithm outlined in section 2.1 of SP 800-22 Rev. 1a. Checks that the numbers of 0 and 1 bits in the produced values are equal to within a certain tolerance.


Provided under the terms of the MIT license. See LICENSE.txt for more information.

Msp430 Rng

Random (SLAA338) and pseudorandom (LCG) number generation.

Msp430 Rng Info

⭐ Stars19
🔗 Source
🕒 Last Update2 years ago
🕒 Created12 years ago
🐞 Open Issues0
➗ Star-Issue RatioInfinity
😎 Author0