[英]Random Number Generator Within Given Range
I'm trying to write a program that uses a function to generate 10 random numbers within a range provided by the user. 我正在尝试编写一个程序,该程序使用一个函数在用户提供的范围内生成10个随机数。 It seems to work okay, other than the fact that the numbers returned are all 1's:
除了返回的数字全为1之外,这似乎还行得通:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rand_int(int min, int max);
int main()
{
int min, max;
cout << "Hello user.\n\n"
<< "This program will generate a list of 10 random numbers within a
given range.\n"
<< "Please enter a number for the low end of the range: ";
cin >> min;
cout << "You entered " << min << ". \n"
<< "Now please enter a number for the high end of the range: ";
cin >> max;
while(min > max){
cout << "Error: Your low number is higher than your high number.\n"
<< "Please reenter your high number, or press ctrl + c
to end program.\n";
cin >> max;
cout << endl;
}
for(int i = 0; i < 10; i++){
int rand_int(int min, int max);
cout << rand_int << endl;
}
return 0;
}
int rand_int(int min, int max)
{
srand(time(0)); // Ensures rand will generate different numbers at different times
int range = max - min;
int num = rand() % (range + min);
return num;
}
Having warnings turned on may have helped here, with -Wall
flag gcc
tells us: 启用警告可能对这里有所帮助,
-Wall
标志gcc
告诉我们:
warning: the address of 'int rand_int(int, int)' will always evaluate as 'true' [-Waddress]
cout << rand_int << endl;
^
Although clang
gives a warning without the need to add flags. 尽管
clang
发出警告,但无需添加标志。 You are using a function pointer here and since std::cout does not have an overload for a function pointer it is selecting the bool overload and converting the function pointer to true
. 您在此处使用函数指针,因为std :: cout没有函数指针的重载 ,因此它正在选择bool重载并将函数指针转换为
true
。 The calls should be like this: 调用应如下所示:
std::cout << rand_int(min, max) <<std::endl;
Although that will not totally fix your issues, you also need to move: 尽管这不能完全解决您的问题,但您还需要移动:
srand(time(0));
outside your function preferably at the start of your program. 超出您的功能范围,最好是在程序开始时。 Since you are calling
rand_int
ten times very quickly the result of time(0)
will probably be the same and therefore you will return the same 10
numbers. 由于您要非常快地调用
rand_int
十次,因此time(0)
的结果可能会相同,因此您将返回相同的10
数字。
This line: 这行:
int rand_int(int min, int max);
in the for loop is just a redeclaration of the function and is not needed. 在for循环中只是函数的重新声明,不需要。
Although, if C++11 is an option using the random header makes way more sense and is much simpler: 虽然,如果使用C ++ 11作为选项,则使用随机标头会更有意义并且更简单:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_int_distribution<int> dist(1,10);
for (int n = 0; n < 10; ++n) {
std::cout << dist(e2) << ", " ;
}
std::cout << std::endl ;
}
If C++11 is not an option then your should at least check out the How can I get random integers in a certain range? 如果没有C ++ 11,那么您至少应该检查一下如何获得一定范围内的随机整数? C FAQ entry which gives the following formula for generating numbers in the range
[M, N]
: C FAQ条目,提供用于生成
[M, N]
范围内的数字的以下公式:
M + rand() / (RAND_MAX / (N - M + 1) + 1)
M + rand()/(RAND_MAX /(N-M +1)+1)
and of course there is always boost: 当然总会有助力:
#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
int main()
{
boost::random::mt19937 gen;
boost::random::uniform_int_distribution<> dist(1, 10);
for (int n = 0; n < 10; ++n) {
std::cout << dist(gen) << ", ";
}
std::cout << std::endl ;
}
Try changing this: 尝试更改此:
for(int i = 0; i < 10; i++){
int rand_int(int min, int max);
cout << rand_int << endl;
}
to: 至:
for(int i = 0; i < 10; i++){
int myRandomNumber = rand_int(int min, int max);
cout << myRandomNumber << endl;
}
It seems that you were outputting the function rather than the return result of it. 似乎您是在输出函数,而不是函数的返回结果。
The Fastest & Simplest way to get the Random Number within a range is- 获取范围内随机数的最快和最简单的方法是-
int lower=1,upper=10;//for example
srand(time(0));
int y = (rand() % (upper-lower + 1)) + lower;
It will give you the output in the range- [1,10] (both inclusion). 它将为您提供范围为[1,10](均包括在内)的输出。
That's it. 而已。 Cheers!
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.