C 如何使用当前时间创建随机数

C 如何使用当前时间创建随机数,c,random,C,Random,我有4个字节的空间来创建一个随机数。最后一个字节用于容器和插槽(与此问题无关),因此我有3个字节来填充当前时间(epoch也可以) 到目前为止,我有以下几点: uint32_t create_handle(uint8_t container_id, uint8_t file_slot) { uint32_t handle; time_t now = time(NULL); handle = ((now << 24) & ~(0xFF)); h

我有4个字节的空间来创建一个随机数。最后一个字节用于容器和插槽(与此问题无关),因此我有3个字节来填充当前时间(epoch也可以)

到目前为止,我有以下几点:

uint32_t create_handle(uint8_t container_id, uint8_t file_slot)
{
    uint32_t handle;

    time_t now = time(NULL);
    handle = ((now << 24) & ~(0xFF));
    handle += (container_id << 4) + file_slot; /* Last byte is 4 bits of container + 4 bits of slot */
    return handle;
}
uint32创建句柄(uint8容器id,uint8文件槽)
{
uint32_t手柄;
time\u t now=时间(空);

handle=((现在你现在移动24,只剩下8。我想你只需要移动8。
&~(0xff)
是多余的


使用其中一个随机数库如何?或者你永远不需要每秒超过一个句柄吗?

srand(time(NULL))
提供随机种子。
rand()
获取随机数。

为什么不使用
rand()
?时间远非随机。左移总是填入0,不需要
&~(0xFF)
是的,我不需要超过一秒钟的时间。要求是我使用当前时间