how to generate a random number--MC1321x

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

how to generate a random number--MC1321x

1,373件の閲覧回数
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 返答(返信)

403件の閲覧回数
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 件の賞賛

403件の閲覧回数
Beijing2008
Contributor I
Dear bigmac:
 
   I got it now,thanks for your support and help!
 
Best regards,
Charly
 
 
0 件の賞賛