如何添加随机数 [英] How to add random numbers
问题描述
我正在尝试做一个数学辅导程序,它会生成从1到50的两个随机数并将它们相加。我不知道从哪里开始。
感谢任何输入。
I am trying to do a math tutor program which generates two random numbers from 1 to 50 and add them up. I have no clue as to where to start.
Any input is appreciated.
推荐答案
看看rand()和srand()函数;两者都在stdlib.h中声明。
阅读所有关于它们这里。
亲切的问候,
Jos
Have a look at the rand() and srand() functions; both are declared in stdlib.h.
Read all about them here .
kind regards,
Jos
看看rand()和srand()函数;两者都在stdlib.h中声明。
阅读所有关于它们这里。
亲切的问候,
Jos
Have a look at the rand() and srand() functions; both are declared in stdlib.h.
Read all about them here .
kind regards,
Jos
好的,谢谢。我会让你有幸在完成后看到我的节目。 : - )
Okay, thanks. I will let you have the honor of seeing my program when it is completed. :-)
查看rand()和srand()函数;两者都在stdlib.h中声明。
阅读所有关于它们这里。
亲切的问候,
Jos
Have a look at the rand() and srand() functions; both are declared in stdlib.h.
Read all about them here .
kind regards,
Jos
嗯,我试过,这就是发生的事情:
程序
#include< iostream>
#include< cstdlib>
#include< conio>
using namespace std;
int main()
{
未签名种子;
int num;
num = 1 + rand() %500;
srand(种子);
cout<< rand()<< endl;
cout<< rand()<< endl;
cout<< rand()<< endl;
getch();
返回0;
}
输出:
130
10982
1090
每次我运行这个程序,我得到相同的三个随机数。什么是交易
Well, I tried, and this is what happened:
Program
#include <iostream>
#include <cstdlib>
#include <conio>
using namespace std;
int main()
{
unsigned seed;
int num;
num = 1 + rand() %500;
srand (seed);
cout << rand() <<endl;
cout << rand() <<endl;
cout << rand() <<endl;
getch();
return 0;
}
Output:
130
10982
1090
Each time I run this program, I get the same three random numbers. what''s the deal
这篇关于如何添加随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!