我的代码
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
float randomnumber(double x)
{
// when input a number(x). Ex 99.5
// this function will return ramdom a (double)number between 98.2%*x to 101.8%*x
double a = 0;
srand(time(0));
a = (1 + rand() % 36) / 1000;
return x * (0.982 + a);
}
float _stdcall randnumber(double x)
{
double a = 0;
do
{
a = randomnumber(x);
}while (a > 100 || a == 100);
return a;
}def文件
LIBRARY "square"
EXPORTS
RandNum = randnumber我在excel-vba中调用它作为函数。当我使用它的时候,它在第一次使用时是正确的,但是...当我手动计算工作簿(Fress F9或重新输入)时,它不会更改编号。为什么它不起作用?
发布于 2016-05-24 19:41:11
srand( time (NULL))基于以秒为单位的当前时间。因此,如果您在不到一秒的时间内调用它,它将为您提供相同的种子。
要解决这个问题,可以提高srand使用的时间结构的精度。
#include <sys/time.h>
struct timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);此答案提供了有关subject的更多信息
https://stackoverflow.com/questions/37409511
复制相似问题