C 语言中的 Intel RdRand 工作示例。如何生成 -100.001 到 +100.001 范围内的浮点数
Working example Intel RdRand in C language. How to generate a float type number in the range -100.001 through +100.001
有一个英特尔 DRNG 库允许您使用基于处理器 crystal 熵效应的随机数生成器。
库中有一个示例,它只打印随机生成的数组的内容。
请分享 C 中的工作示例,它允许使用此库生成 -100.001 到 +100.001 范围内的浮点型数字
我只能找到一个基于伪随机数生成器的代码,但这不是我需要的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float randoms(float min, float max)
{
return (float)(rand())/RAND_MAX*(max - min) + min;
}
int main()
{
srand((unsigned int)time(0));
printf("%f\n",randoms(-100.001, 100.001));
return 0;
}
提前致谢。
答案已经发布 on the Intel's DRNG page 不久前。我想在这里引用它:
You can almost use that same algorithm. You just need a way to check
for the (highly unlikely) chance the RDRAND instruction will not
return a value.
Here's how I would modify your code snippet for Linux (you'll need to
supply the -mrdrnd option to gcc to compile this):
#include <stdio.h>
#include <limits.h>
char randoms(float *randf, float min, float max)
{
int retries= 10;
unsigned long long rand64;
while(retries--) {
if ( __builtin_ia32_rdrand64_step(&rand64) ) {
*randf= (float)rand64/ULONG_MAX*(max - min) + min;
return 1;
}
}
return 0;
}
int main()
{
float randf;
if ( randoms(&randf, -100.001, 100.001) ) printf("%f\n", randf);
else printf("Failed to get a random value\n");
return 0;
}
See section 4.2.1 in the above document:
4.2.1 Retry Recommendations
It is recommended that applications attempt 10 retries in a tight loop
in the unlikely event that the RDRAND instruction does not return a
random number. This number is based on a binomial probability
argument: given the design margins of the DRNG, the odds of ten
failures in a row are astronomically small and would in fact be an
indication of a larger CPU issue.
有一个英特尔 DRNG 库允许您使用基于处理器 crystal 熵效应的随机数生成器。
库中有一个示例,它只打印随机生成的数组的内容。
请分享 C 中的工作示例,它允许使用此库生成 -100.001 到 +100.001 范围内的浮点型数字
我只能找到一个基于伪随机数生成器的代码,但这不是我需要的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float randoms(float min, float max)
{
return (float)(rand())/RAND_MAX*(max - min) + min;
}
int main()
{
srand((unsigned int)time(0));
printf("%f\n",randoms(-100.001, 100.001));
return 0;
}
提前致谢。
答案已经发布 on the Intel's DRNG page 不久前。我想在这里引用它:
You can almost use that same algorithm. You just need a way to check for the (highly unlikely) chance the RDRAND instruction will not return a value.
Here's how I would modify your code snippet for Linux (you'll need to supply the -mrdrnd option to gcc to compile this):
#include <stdio.h>
#include <limits.h>
char randoms(float *randf, float min, float max)
{
int retries= 10;
unsigned long long rand64;
while(retries--) {
if ( __builtin_ia32_rdrand64_step(&rand64) ) {
*randf= (float)rand64/ULONG_MAX*(max - min) + min;
return 1;
}
}
return 0;
}
int main()
{
float randf;
if ( randoms(&randf, -100.001, 100.001) ) printf("%f\n", randf);
else printf("Failed to get a random value\n");
return 0;
}
See section 4.2.1 in the above document:
4.2.1 Retry Recommendations
It is recommended that applications attempt 10 retries in a tight loop in the unlikely event that the RDRAND instruction does not return a random number. This number is based on a binomial probability argument: given the design margins of the DRNG, the odds of ten failures in a row are astronomically small and would in fact be an indication of a larger CPU issue.