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.

TMS320F280039C: Random number generation

Part Number: TMS320F280039C


Dear Champs,

I am asking this for our customer.

The user wants to generate a random number from a set.

For example, to pick up a random number from integer 1 to 1000.

This is running from C28x.

1. Does F28003x have any HW accelerator for that?

2. If not, how does TI suggest doing so?

3. Bottom line, can the user use the standard C library function int rand(void), which should be defined in 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  • Hi Wayne,

    1. Does F28003x have any HW accelerator for that?

    2. If not, how does TI suggest doing so?

    There is a related post here on how we suggest generating random number from a set.

    3. Bottom line, can the user use the standard C library function int rand(void), which should be defined in 

    User can use the standard C library. Here is another post related to this here and here.

    Best,

    Ryan Ma

  • Dear Ryan,

    OK, I tried with the standard C and it works.

    Test code is listed for those who are interested in this topic.

    //#############################################################################
    //
    // FILE:   main.c
    //
    // TITLE:  Universal LED Project
    //
    // Universal LED Project Example
    //
    // This example demonstrates how to blink an LED using the **Universal Project**.
    // In order to migrate the project within syscfg, click the swtich button under 
    // the device view and select your corresponding device to migrate, saving the
    // project will auto-migrate your project settings.
    //
    //#############################################################################
    //
    //
    // $Copyright:
    // Copyright (C) 2023 Texas Instruments Incorporated - http://www.ti.com/
    //
    // Redistribution and use in source and binary forms, with or without 
    // modification, are permitted provided that the following conditions 
    // are met:
    // 
    //   Redistributions of source code must retain the above copyright 
    //   notice, this list of conditions and the following disclaimer.
    // 
    //   Redistributions in binary form must reproduce the above copyright
    //   notice, this list of conditions and the following disclaimer in the 
    //   documentation and/or other materials provided with the   
    //   distribution.
    // 
    //   Neither the name of Texas Instruments Incorporated nor the names of
    //   its contributors may be used to endorse or promote products derived
    //   from this software without specific prior written permission.
    // 
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    // $
    //#############################################################################
    
    //
    // Included Files
    //
    #include "driverlib.h"
    #include "device.h"
    #include "board.h"
    #include "c2000ware_libraries.h"
    
    #include <stdlib.h>
    #include <time.h>
    
    
    // Main
    //
    void main(void)
    {
    
        //
        // Initialize device clock and peripherals
        //
        Device_init();
    
        //
        // Disable pin locks and enable internal pull-ups.
        //
        Device_initGPIO();
    
        //
        // Initialize PIE and clear PIE registers. Disables CPU interrupts.
        //
        Interrupt_initModule();
    
        //
        // Initialize the PIE vector table with pointers to the shell Interrupt
        // Service Routines (ISR).
        //
        Interrupt_initVectorTable();
    
        //
        // PinMux and Peripheral Initialization
        //
        Board_init();
    
        //
        // C2000Ware Library initialization
        //
        C2000Ware_libraries_init();
    
    
        srand( time(NULL) );
    
        int x = rand();
        int y = rand() % 1000;
        //
        // Enable Global Interrupt (INTM) and real time interrupt (DBGM)
        //
        EINT;
        ERTM;
    
        //
        // Loop Forever
        //
        for(;;)
        {
            //
            // Turn on LED
            //
            GPIO_writePin(myBoardLED0_GPIO, 0);
    
            //
            // Delay for a bit.
            //
            DEVICE_DELAY_US(500000);
    
            //
            // Turn off LED
            //
            GPIO_writePin(myBoardLED0_GPIO, 1);
    
            //
            // Delay for a bit.
            //
            DEVICE_DELAY_US(500000);
        }
    }
    
    //
    // End of File
    //