Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中带有字符数组的Srand?_C_Random_Srand_Random Seed - Fatal编程技术网

C中带有字符数组的Srand?

C中带有字符数组的Srand?,c,random,srand,random-seed,C,Random,Srand,Random Seed,如何使用字符数组而不是C中的int进行随机种子设定? 我想使用密码,而不是数字,但srand只接受整数。 有办法做到这一点吗?只需使用哈希函数即可。经典之作是hash_pjw unsigned hash_pjw (const void *str) { const char *s = str; unsigned int g, h = 1234567u; while (*s != 0) { h = (h << 4) + *s++; if ((g = h

如何使用字符数组而不是C中的int进行随机种子设定?
我想使用密码,而不是数字,但
srand
只接受整数。

有办法做到这一点吗?

只需使用哈希函数即可。经典之作是hash_pjw

unsigned hash_pjw (const void *str)   
{
  const char *s = str;
  unsigned int g, h = 1234567u;
  while (*s != 0) {
    h = (h << 4) + *s++;
    if ((g = h & (unsigned int) 0xf0000000) != 0)
       h = (h ^ (g >> 24)) ^ g;
  }
  return h;
}
unsigned hash_pjw(const void*str)
{
const char*s=str;
无符号整数g,h=1234567u;
而(*s!=0){
h=(h>24))^g;
}
返回h;
}

请注意,
rand()
与加密安全完全不同。

一种方法是创建字符串的散列,并将结果整数用作种子。@cpluplusooaa然后再次阅读问题。这根本不是我要问的。@yentup对我来说,Edward的回答似乎是正确的,即使用哈希函数从密码中获取一个int,并将该int用作srand的种子(通常将该int传递给srand)。@Chnossos你知道最好的哈希函数吗?
srand((未签名)strtoul(“alphanums”,NULL,36))