This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How seed random number?

Other Parts Discussed in Thread: MSP430G2533, MSP430F6779

I'm using LaunchPad/'2533.  I wonder if there's a clever way to seed the random number generator (srand(seed)) that a) works across reset and b) does not require writing flash.  For instance, another MCU I've used has a free running counter that gives me a different seed every reset (not sure if it's actual timing difference or just not initialized).  Is there any similar register in '2533, maybe hidden in the clock generator.  Thought of reading the temp sensor, might work semi-OK, but maybe there's an easier way. BTW, the seed doesn't really need to be very random, just want a seed that has a decent chance of varying across reset.

  • Hey Tom,

    A few clever ways to seed random number generators that I've used over the years are reading from a floating ADC or using a free running timer.  Either way, usually you mask some of the bits.  Lets say you have a 12 bit ADC, I might only look at the last 5-6 bits, because these are the most random. 

    A free running timer only works if you have a user input that can trigger the read at a random time.  If not, you theoretically could know the seed.  Same as above, either used few of the lowest bits for seed or used a bit for a binary state.  Ex. I had to randomly assign Left or rights for 5 stations for a robotics project in college.  I started a free running 16 bit time, and when the user hit the start button, used fixed bits for each station.  Station 1 = bit 13, station 2= bit 14... etc. 

    Hope this helps,

    JD 

  • Hi,

    maybe this can help:

    http://processors.wiki.ti.com/index.php/MSP430_FAQ#How_to_generate_random_number_with_MSP430_devices.3F

    One  basic thing is that most of the MSP430 devices have a VLO oscillator which can be used to generate random numbers due to its physical properties. The VLO is a very low cost, low frequency oscillator which is usually defined to oscillate between 4 - 20 kHz.

  • I should have mentioned 'Power-On RESET' isn't a concern, rather for example, hitting the reset switch powered-up.  In the case of 'POR' could probably get away just reading a RAM location.  Also should have pointed out there's no 'hit a key' or other external randomness I can use.  The floating ADC might be OK, depending on how many bits of jitter - but that might depend on particulars outside my control (power supply quality, PCB layout, packaging, ambient interference, etc).  Fortunately the VLO solution looks like exactly what I was hoping for.  Thanks gentleman!

  • I dropped the code into my ccs project.  Bit of a headscratcher is the compiler says TASSEL__SMCLK and MC__CONTINOUS are undefined (I did include msp430g2533.h and all the other timer symbols are recognized).  So I tried just hardwiring the values ala...

    //   TA0CTL |= TASSEL__SMCLK + MC__CONTINOUS;

     TA0CTL |= 0x220;

    ...but it seems the capture flag never getting set, stuck waiting at...

        while(!(TA0CCTL1 & CCIFG));

    ...checked the data sheet & user manual, don't know why the symbol issue.  PS: I don't have a crystal, is that a problem?  Should I do all this before or after setting the DCO?

  • Teh floatign ADC, sicne it uses charge distribution, won't do good. No connection, no charge -> no reading.
    The VLO too isn't that good. While it is quite random across different devices, it may well give reproduceable results when external circumstances (VCC rising time and level, temperature) are all the same.
    One possible source of randomness is to use the timer external clock pin as antenna and capture radio waves to count the timer. Could work fine and really random, or may fail completely (especially if the device is in a shielded box)

  • ...just to follow-up, discovered for '2533 you have to use CCR0 instead of CCR1.  Seems to work well now, I get random number across reset.  Thanks again!

  • Hello,

    another way is to take multiple sources of randomness, like ADC, external timing measurements, Temperature measurements if available and so on.

    The caught data can be hashed by using a hash algorithm to produce an initial seed for an PRNG like rand().

    The hash algorithm used has to generate a uniform distribution of outputvalues from the random input values. This is nearly impossible, but the goal of a good hashing algorithm is to  get as near as possible to a uniform distribution, so as the PRNG has to do also.

    You can for example use 20 ADC measurements, 5 external timing measurements  and so on as input value.

    One of my favorite hashing algorithms is the sdbm hash of sleepycat (maybe there is a better one).

        static unsigned long
        sdbm(str)
        unsigned char *str;
        {
            unsigned long hash = 0;
            int c;
    
            while (c = *str++)
                hash = c + (hash << 6) + (hash << 16) - hash;
    
            return hash;
        }
    
    
    This function can be rewritten for use with an MCU (to avoid storing the input values into a large RAM area before processing it with the hash algorithm).
    Regards Marco
  • Well,

    i think the basic question is the bargain you can get for every solution. In low-power application, i would say the VLO is the best option rather than the ADC one, since operating ADC will consume much power than only operating a timer module sourced by internal VLO.

  • Hello,

    lhend said:
    In low-power application, i would say the VLO is the best option rather than the ADC one, since operating ADC will consume much power than only operating a timer module sourced by internal VLO.

    But if you just want to get one random number on startup, to be able to seed the PRNG, it does not matter I think. As long as you do not have an energy harvesting system you can use the ADC and other random sources for some 10 milliseconds or less to get the required amount of measurements and then disable it again, if not used for other purposes.

    I will have a look on the VLO solution, as I also did not know it yet. Sounds interesting.

    Regards Marco

  • Hi Leo,

    I tried this code on msp430f6779 mcu.I generated random numbers this fonction(TI_getRandomIntegerFromVLO) interval.It works fine for a while but then it crash. Function always returns -1 not random number.I could not understand why? Can you say something about that?

**Attention** This is a public forum