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.

[参考译文] AM2732:如何在 AM2732项目中添加生成随机数的函数?

Guru**** 2378650 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1375557/am2732-how-to-add-the-function-of-generating-random-numbers-in-the-am2732-project

器件型号:AM2732

工具与软件:

我们有一个生成随机数据的函数。  我尝试了导入 C 库函数。  在代码中使用以下语句:

srand (time (NULL));

unsigned char num = rand ()%256;

但每次生成的值是固定的。

我应该怎么做才能正确生成随机数?

谢谢。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    可能是这样:

    https://c-faq.com/lib/notveryrand.html

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    此外、您只需要使用 srand ()一次。 没有理由继续设置种子。

    您是否确定 Time (NULL)正在返回一个"随机"种子?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我尝试了上面提到的方法, 用系统的 API: CycleCounterP_getCount32()替换了时间(NULL),它现在工作正常。

    非常感谢。