我找不到任何解决方案来生成范围内的随机浮点数[0,a]
,其中a
是用户定义的一些浮点数。
我尝试了以下方法,但似乎无法正常工作。
float x=(float)rand()/((float)RAND_MAX/a)
我找不到任何解决方案来生成范围内的随机浮点数[0,a]
,其中a
是用户定义的一些浮点数。
我尝试了以下方法,但似乎无法正常工作。
float x=(float)rand()/((float)RAND_MAX/a)
尝试:
float x = (float)rand()/(float)(RAND_MAX/a);
要了解其工作原理,请考虑以下内容。
N = a random value in [0..RAND_MAX] inclusively.
上面的等式(为了清楚起见去掉了铸件)变为:
N/(RAND_MAX/a)
但是除以分数相当于乘以所述分数的倒数,所以这相当于:
N * (a/RAND_MAX)
可以重写为:
a * (N/RAND_MAX)
考虑到N/RAND_MAX
始终是介于 0.0 和 1.0 之间的浮点值,这将生成介于 0.0 和 之间的值a
。
Alternatively, you can use the following, which effectively does the breakdown I showed above. I actually prefer this simply because it is clearer what is actually going on (to me, anyway):
float x = ((float)rand()/(float)(RAND_MAX)) * a;
Note: the floating point representation of a
must be exact or this will never hit your absolute edge case of a
(it will get close). See this article for the gritty details about why.
Sample
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand((unsigned int)time(NULL));
float a = 5.0;
for (int i=0;i<20;i++)
printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);
return 0;
}
Output
1.625741
3.832026
4.853078
0.687247
0.568085
2.810053
3.561830
3.674827
2.814782
3.047727
3.154944
0.141873
4.464814
0.124696
0.766487
2.349450
2.201889
2.148071
2.624953
2.578719
You can also generate in a range [min, max] with something like
float float_rand( float min, float max )
{
float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
return min + scale * ( max - min ); /* [min, max] */
}
这会在两个浮点数之间生成一个随机浮点数。
float RandomFloat(float min, float max){
return ((max - min) * ((float)rand() / RAND_MAX)) + min;
}
while it might not matter now here is a function which generate a float between 2 values.
#include <math.h>
float func_Uniform(float left, float right) {
float randomNumber = sin(rand() * rand());
return left + (right - left) * fabs(randomNumber);
}
If you want to generate a random float in a range, try a next solution.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float
random_float(const float min, const float max)
{
if (max == min) return min;
else if (min < max) return (max - min) * ((float)rand() / RAND_MAX) + min;
// return 0 if min > max
return 0;
}
int
main (const int argc, const char *argv[])
{
srand(time(NULL));
char line[] = "-------------------------------------------";
float data[10][2] = {
{-10, 10},
{-5., 5},
{-1, 1},
{-0.25, -0.15},
{1.5, 1.52},
{-1700, 8000},
{-0.1, 0.1},
{-1, 0},
{-1, -2},
{1.2, 1.1}
};
puts(line);
puts(" From | Result | To");
puts(line);
int i;
for (i = 0; i < 10; ++i) {
printf("%12f | %12f | %12f\n", data[i][0], random_float(data[i][0], data[i][1]), data[i][1]);
}
puts(line);
return 0;
}
A result (values is fickle)
-------------------------------------------
From | Result | To
-------------------------------------------
-10.000000 | 2.330828 | 10.000000
-5.000000 | -4.945523 | 5.000000
-1.000000 | 0.004242 | 1.000000
-0.250000 | -0.203197 | -0.150000
1.500000 | 1.513431 | 1.520000
-1700.000000 | 3292.941895 | 8000.000000
-0.100000 | -0.021541 | 0.100000
-1.000000 | -0.148299 | 0.000000
-1.000000 | 0.000000 | -2.000000
1.200000 | 0.000000 | 1.100000
-------------------------------------------