how to generate a random number--MC1321x

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to generate a random number--MC1321x

1,388 Views
Beijing2008
Contributor I
Dear all:
 
I'm using MC13213 and CodeWarrior6.1,now I want to get a random number with following C code:
main()
{
clock_t clk;
unsigned int random_number;
EnableInterrupts;
clk = clock();               //clock always return zero,so clk is equal to zero
srand((int)clk);             //feed always is zero
random_number = rand();      //the random_number is a constant every time running, not a random
......
}

I don't konw why the clock always return 0,how to solve it ?
Otherwise,call the time(),it also returns 0.
 
look forward to your suggestion!
Thanks and best regards!
Charly

 
Labels (1)
0 Kudos
2 Replies

418 Views
bigmac
Specialist III
Hello Charly,
 
I believe that the problem may be due to the clock() function not being implemented within the C library, because of its hardware dependency.  You would need to write your own function to suit the device you are using.
 
However, a simpler method might be the following direct use of the TPM count value.
srand( TPM1CNT);
 
This would assume that the time when the TPM count is read can be "randomised", perhaps by an external event, otherwise the seed value would again alway be the same value.
 
Regards,
Mac
 
0 Kudos

418 Views
Beijing2008
Contributor I
Dear bigmac:
 
   I got it now,thanks for your support and help!
 
Best regards,
Charly
 
 
0 Kudos