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.

Random Number Generation

Hello,

I am currently trying to generate random numbers on the TI Connected Launchpad.  I have found a few examples for the MSP430 that rely on an onboard temperature sensor to generate random numbers, and I was wondering if there was a way to do this on the Cortex CPUs as well.  Is this approach used on the Cortex CPUs through the ADC_CTL_TS register, or is there another approach that is commonly used on this device for generating random numbers?  

Thanks,

Logan

  • Hello Logan,

    C library has rand() function call which can be seeded with something like the temperature sensor.

    Regards
    Amit
  • Hello Amit,

    Do you have a code sample that shows how to seed the srand() function call with this data? Do we just use the ADC_CTL_TS register directly? Also, does that register provide a suitable seed to produce psuedo-random numbers?

    Thanks,
    Logan
  • Hello Logan,

    I do not have a code example which uses TS register output. But as working example, if I want to have a predictable random data pattern then I would use something as below where a 8-bit array is being prefilled with a random data (to cap the value I am using the remainder of the rand mod 255)

    //
    // PreFill Slave TX Buffer
    //
    for(ui32Index=0;ui32Index<I2C0_RXBUF_SIZE;ui32Index++)
    {
    g_ui8STxBuf[ui32Index] = rand()%255;
    }

    In your case it may be more like

    srand(g_ui32TempSense);
    //
    // PreFill Slave TX Buffer
    //
    for(ui32Index=0;ui32Index<I2C0_RXBUF_SIZE;ui32Index++)
    {
    g_ui8STxBuf[ui32Index] = rand()%255;
    }


    Regards
    Amit
  • Hi Amit,

    Thank you for your response. I was wondering what you have g_ui32TempSense set to?

    Regards,
    Logan
  • Hello Logan,

    The value of g_ui32TempSense is the converted value from ADC's Temp Sense. This can be a one time event at start up to give a randomness to the subsequent rand function call

    Regards
    Amit