Jump to content

Static random number generator

Recommended Posts

In an attempt to port one of my projects from AVR to the MSP430G2553 and failing horribly because the printf() function in CCS does not fit into the 512 bytes RAM, I did manage to port a single function.
The function, READrnd(), reads some pre generated random numbers from flash memory. I use them to give my projects some (predefined) randomness. For example, the project I want to port uses the predefined random numbers to "randomly" turn on and off 8 relays. I am going to use it for some night/day simulation in houses on a model railroad track.
Anyway, with the MSP430 version of the online number generator you can generate random numbers. Here are the sources.

/* generated using: http://atoomnet.net/?p=462 */


//32 guaranteed random numbers
const uint8_t random0[] PROGMEM = {
  0x87, 0x22, 0xFB, 0x0D, 0x23, 0x03, 0x60, 0x3F, 0x7A, 0x65, 0xC5, 0xF2, 0x10, 0xF2, 0x2A, 0xB3, 0x7D, 0xD4, 0x5D, 0x8B, 0xD5, 0xF5, 0x1B, 0x4C, 0xC7, 0x45, 0xAF, 0x5E, 0x81, 0xAB, 0x71, 0xF8, 

#endif /* RANDOMVALS_H_ */


#ifndef RANDOM_H_
#define RANDOM_H_

#include <stdint.h>

uint8_t READrnd(void);

#endif /* RANDOM_H_ */


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

With these files you have (in this example) 32 bytes of random numbers to work with.


Here is the example program running on the TI launchpad I used to test the above sources. Led1 and led2 will flash seemingly random.



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


All settings are default except the optimization: -O4 --opt_for_speed=0
The asm("nop") forces the compiler to not optimize away the (for it) pointless for-loop.


I hope you like it and, as a beginner in the MSP430 architecture all feedback is welcome.

Link to post
Share on other sites

A fixed set of numbers can work in some cases, but I think srand() and rand() are more commonly used for this sort of thing. Are they missing from CCS's libraries?

embtextf might help with your printf size issues. If you use CCS as an IDE you might have to run the configure step manually to create the header, but from that point you should be able to just add the sources to your project.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...