C语言 在 C 中生成随机的 32 位十六进制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7622887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating a random 32 bit hexadecimal value in C
提问by sachin
What would be the best way to generate a random 32-bit hexadecimal value in C? In my current implementation I am generating each bit separately but the output is not completely random ... many values are repeated several times. Is it better to generate the entire random number instead of generating each bit separately?
在 C 中生成随机 32 位十六进制值的最佳方法是什么?在我当前的实现中,我分别生成每个位,但输出不是完全随机的……许多值重复多次。生成整个随机数而不是单独生成每一位更好吗?
The random number should make use of the entire 32 bit address space (0x00000000 to 0xffffffff)
随机数应该使用整个 32 位地址空间(0x00000000 到 0xffffffff)
file = fopen(tracefile,"wb"); // create file
for(numberofAddress = 0; numberofAddress<10000; numberofAddress++){ //create 10000 address
if(numberofAddress!=0)
fprintf(file,"\n"); //start a new line, but not on the first one
fprintf(file, "0 ");
int space;
for(space = 0; space<8; space++){ //remove any 0 from the left
hexa_address = rand() % 16;
if(hexa_address != 0){
fprintf(file,"%x", hexa_address);
space++;
break;
}
else if(hexa_address == 0 && space == 7){ //in condition of 00000000
fprintf(file,"%x", "0");
space++;
}
}
for(space; space<8; space++){ //continue generating the remaining address
hexa_address = rand() % 16;
fprintf(file,"%x", hexa_address);
}
}
回答by Mysticial
x = rand() & 0xff;
x |= (rand() & 0xff) << 8;
x |= (rand() & 0xff) << 16;
x |= (rand() & 0xff) << 24;
return x;
rand()doesn't return a full random 32-bit integer. Last time I checked it returned between 0and 2^15. (I think it's implementation dependent.) So you'll have to call it multiple times and mask it.
rand()不返回完全随机的 32 位整数。上次我检查它在0和之间返回2^15。(我认为它依赖于实现。)所以你必须多次调用它并屏蔽它。
回答by jailaxmi k
Do this way.It creates a bigger number than the earlier logic .If you are interested the MSB then the below logic is good .:
这样做。它创建一个比早期逻辑更大的数字。如果您对 MSB 感兴趣,那么下面的逻辑很好。:
/** x = rand() ^ rand()<<1; **/
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <string>
#include <stdio.h>
int main () {
int i, n;
n = 50;
uint x,y ;
//4294967295 :UNIT_MAX
/* Intializes random number generator */
srand((unsigned) time(0));
for( i = 0 ; i < n ; i++ ) {
/**WAY 1 **/
x = rand() ^ rand()<<1;
printf("x:%u\t",x);
printf("Difference1:(4294967295 - %u) = %u\n",x,(4294967295 - x));
/**WAY 2 **/
y = rand() & 0xff;
y |= (rand() & 0xff) << 8;
y |= (rand() & 0xff) << 16;
y |= (rand() & 0xff) << 24;
printf("y:%u\t",y);
printf("Difference2:(4294967295 - %u) = %u\n",y,(4294967295 - y));
printf("Difference between two is = %u\n",(x) - (y));
}
printf("End\n");
return(0);
}
回答by Kerrek SB
You can just create anyrandom number that's at least 32 bit wide and format that as hex. Examples:
您可以创建任何至少 32 位宽的随机数并将其格式化为十六进制。例子:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
uint32_t n;
n = mrand48(); // #1
n = rand(); // #2
FILE * f = fopen("/dev/urandom", "rb");
fread(&n, sizeof(uint32_t), 1, f); // #3
// ... etc. etc. E.g. Windows Crypto API
char hex[9];
sprintf(hex, "%08X", n);
Now hexis a string containing eight random hexadecimal digits. Don't forget to seed the various pseudo random number generators (using srand48()and srand(), respectively, for #1 and #2). Since you'll essentially have to seed the PRNGs from random source with at least one 32-bit integer, you might as well tap the random source directly (unless you're using time()or something "non-random" like that).
现在hex是一个包含八个随机十六进制数字的字符串。不要忘记为各种伪随机数生成器设置种子(分别为 #1 和 #2使用srand48()和srand())。由于您基本上必须使用至少一个 32 位整数从随机源中播种 PRNG,因此您不妨直接点击随机源(除非您正在使用time()或类似的“非随机”)。

