编写程序输出在指定范围内的3个随机数,范围在文本框中输入
1个回答
展开全部
下面是范围0.0到1.0的随机数产生方法.供参考.
指定范围若是 [a,b] 且 b > a
计算三次:
---------------------------------------------
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x1 = r * (b-a) + a;
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x2 = r * (b-a) + a;
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x3 = r * (b-a) + a;
----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
main()
{
int seed; // 种子
double r;
seed = 10000; // 随便给个种子
srand(seed); // 初始化随机数生成器
// r 是双精度浮点随机数,范围0.0到1.0
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
}
自己添入a,b,x1,x2,x3的类型声明
自己添入a,b的输入语句
指定范围若是 [a,b] 且 b > a
计算三次:
---------------------------------------------
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x1 = r * (b-a) + a;
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x2 = r * (b-a) + a;
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x3 = r * (b-a) + a;
----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
main()
{
int seed; // 种子
double r;
seed = 10000; // 随便给个种子
srand(seed); // 初始化随机数生成器
// r 是双精度浮点随机数,范围0.0到1.0
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
}
自己添入a,b,x1,x2,x3的类型声明
自己添入a,b的输入语句
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询