输入A和B的值,然后产生A到B间的随机数?
输入A和B的值,然后产生A到B间的随机数? 问题点数:20、回复次数:12Top
1 楼xuzheng318(忧郁王子)回复于 2004-12-01 11:56:27 得分 2
#define NUM 6 /*1-6之间的随机数*/
int main(){
int i=0;
int e=0;
srand(time());//如果少了这句话,你生成的随机数将会是每次运行都是一样的!
for(; i<10; i++){
e=rand()%NUM+1;
printf("%d\n", e);
}
printf("\n\n");
return 1;
printf("%s",__TIME__);
}Top
2 楼fallhunter(不乖)回复于 2004-12-01 12:09:16 得分 3
其实就是产生一个从0到abs(A-B)之间的随机数啊
int interval = abs(A-B); // A和B的差
int small = A>B ? B:A; // 小的数
int rand_number = small + rand()%interval;Top
3 楼xu233(飞天)回复于 2004-12-01 12:10:59 得分 0
rand()/(B-A)+A ;Top
4 楼xu233(飞天)回复于 2004-12-01 12:14:40 得分 0
刚才产生的随机数包括了A没包括B。
Top
5 楼xu233(飞天)回复于 2004-12-01 12:20:16 得分 0
产生的随机数包含A和B:
前提条件是B>A ;
rand()/(B-A+1)+A ;
不包含A和B:
前提条件是(B-A)>2:
rand()/(B-A-1) + (A+1);Top
6 楼sms88(白板http://shop34112882.taobao.com)回复于 2004-12-01 21:17:30 得分 0
这样也根本不行
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int i,n;
int lbound, ubound;
srand((unsigned)time(NULL));
for (i=0; i < 10; i++)
{
printf("please input the lbound\n");
scanf("%d",&lbound);
printf("please input the ubound\n");
scanf("%d",&ubound);
n=rand()/(ubound-lbound)+lbound;
printf("the nummber is %d\n",n);
}
}
Top
7 楼zchzch(暂伴月将影)回复于 2004-12-01 21:26:04 得分 0
呵呵,来迟了。Top
8 楼jp1984(mathfrog)回复于 2004-12-01 22:27:41 得分 10
rand() % a + b;Top
9 楼baryjim(吃饭-睡觉-打豆豆)回复于 2004-12-01 23:24:00 得分 0
一般都无法保证分布的均匀性,以时间为种子Top
10 楼Leaveye(~枝)(男子无权便是钱)回复于 2004-12-02 00:50:34 得分 5
(A, B): (rand() + 1.0) / (RAND_MAX + 2.0) * (B - A) + A
[A, B): ((double) rand()) / (RAND_MAX + 1.0) * (B - A) + A
(A, B]: (rand() + 1.0) / (RAND_MAX + 1.0) * (B - A) + A
[A, B]: ((double) rand()) / RAND_MAX * (B - A) + A
⒈ 随机程度依赖于宏 RAND_MAX 对应数字的大小。
⒉ 使用前要初始化随机种子。如:srand((unsigned) time(NULL));
Top
11 楼lswx(柳树)回复于 2004-12-02 10:29:23 得分 0
听课ing...Top
12 楼newPhoenix(进退两难)回复于 2004-12-02 11:18:45 得分 0
天天在听课ing~~~~~~~~~~~~~~~~~Top




