我编写了这个函数来获取一个介于0和1之间(包括0和1)的伪随机浮点数:
float randomFloat()
{
float r = (float)rand()/(float)RAND_MAX;
return r;
}
然而,它始终返回0.563585。无论我运行控制台应用程序多少次,这个数字都相同。
编辑:
如果需要,这是我的整个应用程序:
#include <stdio.h>
#include <stdlib.h>
float randomFloat()
{
float r = (float)rand() / (float)RAND_MAX;
return r;
}
int main(int argc, char *argv[])
{
float x[] = {
0.72, 0.91, 0.46, 0.03, 0.12, 0.96, 0.79, 0.46, 0.66, 0.72, 0.35, -0.16,
-0.04, -0.11, 0.31, 0.00, -0.43, 0.57, -0.47, -0.72, -0.57, -0.25,
0.47, -0.12, -0.58, -0.48, -0.79, -0.42, -0.76, -0.77
};
float y[] = {
0.82, -0.69, 0.80, 0.93, 0.25, 0.47, -0.75, 0.98, 0.24, -0.15, 0.01,
0.84, 0.68, 0.10, -0.96, -0.26, -0.65, -0.97, -0.03, -0.64, 0.15, -0.43,
-0.88, -0.90, 0.62, 0.05, -0.92, -0.09, 0.65, -0.76
};
int outputs[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
int patternCount = sizeof(x) / sizeof(int);
float weights[2];
weights[0] = randomFloat();
weights[1] = randomFloat();
printf("%f\n", weights[1]);
float learningRate = 0.1;
system("PAUSE");
return 0;
}
drand48()
函数族:http://www.opengroup.org/onlinepubs/9699919799/functions/drand48.html - Jonathan Leffler