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 to migrate the random number generator



One random number generator code is as follow:

#include<stdio.h>
#include<stdlib.h>
int RandomNumberGenerator(int min_num, int max_num);

int main(void) {
printf("Min : 1 Max : 30 %d\n",random_number(0,5));
printf("Min : 100 Max : 1000 %d\n",random_number(100,1000));
return 0;
}

int RandomNumberGenerator(int min_num, int max_num)
{
int result=0,low_num=0,hi_num=0;
if(min_num<max_num)
{
low_num=min_num;
hi_num=max_num+1; // this is done to include max_num in output.
}else{
low_num=max_num+1;// this is done to include max_num in output.
hi_num=min_num;
}
srand(time(NULL));
result = (rand()%(hi_num-low_num))+low_num;
return result;
}

Put it into the CCS 5.2.1 with RM46 core,

the error messages appear as following:

"#10192 Failed linktime optimization"

"symbol "time" redeclared with incompatible type:"

When changing the time function to clock

like as "srand(clock(NULL));", the error exists still.

How to do this?

Thanks.

  • Richard,

    With the same start seed, the rand() returns a new value each time it is called. If you are not looking for some specific statistical properties. I would suggest you removing the call of srand(). Since the generation of random numbers is computationally intensive, I would suggest creating the table offline and save it to a table to be used in real time.

    I think that a user needs to set up the clock() or time() function with the timers hardware on the chip. Compiler does not know what timer is available on the device.

    Thanks and regards,

    Zhaohong

  • Can I use this code as the following:

    int random_number(int min_num, int max_num)

    {

      #include <stdlib.h> int result=0,low_num=0,hi_num=0;           

     if(min_num<max_num)           

     {                low_num=min_num;               

                      hi_num=max_num+1;

      }// this is done to include max_num in output. 

      else

     {          

           low_num=max_num+1;// this is done to include max_num in output.             

           hi_num=min_num;  

      }  

                        srand(rand());            

                         return result = (rand()%(hi_num-low_num))+low_num;           

    }//END_OF_Function_random_number

  • I don't know if anyone still cares about this post, but I'm surprised no one caught this one:

    Calling srand(rand()) should guarantee that you get the same value each time:

    from the rand() man page:

    If no seed value is provided, the rand() function is automatically seeded with a value of 1.

    Depending on how strong you need your random number sequence to be, you can experiment with getting seed data from sensors or timers.

  • Thanks for your reply.

    I do this by a nonlinear oscillator as a timer independent of MCU functions.

    Via ADC, it gets a random number in each sample.

  • Richard,

    What is the status on you thread? Did your question answered? If so can you mark it as verified answer so we can close it?