how to generate a random number--MC1321x

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

how to generate a random number--MC1321x

1,387 次查看
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

 
标签 (1)
0 项奖励
2 回复数

417 次查看
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 项奖励

417 次查看
Beijing2008
Contributor I
Dear bigmac:
 
   I got it now,thanks for your support and help!
 
Best regards,
Charly
 
 
0 项奖励