我需要从字符串的散列中生成一个种子。此种子将用于生成随机数,我可能使用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位。

09-28 01:56