gpt4 book ai didi

c - 使用 char 缓冲区随机播种

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:39 25 4
gpt4 key购买 nike

我需要从字符串的散列生成种子。该种子将用于生成随机数,我可能会使用 srandom(),但是该函数不将字符缓冲区作为参数,它采用整数。有什么方法可以使用 char 缓冲区为 srandom 播种,或者有什么其他方法可以从哈希键生成 int 种子。希望对您有所帮助!

最佳答案

char *hashKey = "helloWorld!"; /* your Hash pointer */

int intHash = 0;
int hashCarry = 0;
for(int i = 0; hashKey[i] != '\0'; i++){

intHash ^= hashKey[i]; // XORing with current character

hashCarry = intHash & 0xF0000000; /* getting 4 bits - MSBs */

intHash <<= 4; // Multiplying it by 16

intHash += hashCarry >> 28; /* e.g 0xF0000000 becomes 0x0000000F */
}

srandom(intHash);

希望这有助于...

上述代码不会破坏您的任何 HashKey 位。

关于c - 使用 char 缓冲区随机播种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com