[英]c++ test if random number is positive or negative
I have school work with C++ and Im new to it.我有 C++ 的学校作业,我是新来的。 I have to generate 17 random numbers and then test if they are positive/negative/equal to zero.
我必须生成 17 个随机数,然后测试它们是否为正/负/等于零。 I can't seem to find a way to take the random generated number from the for loop to test it in the if/else if loop.
我似乎无法找到一种方法来从 for 循环中获取随机生成的数字以在 if/else if 循环中对其进行测试。 Heeeeelp please:D (The b variable is just there from tests)
Heeeeeelp 请:D(b 变量就在测试中)
#include <ctime>
using namespace std;
int main() {
srand(
(unsigned) time(0)
);
int a;
int b;
int num;
for(int a = 0; a<17; a++) {
cout << "Skaitlis: " << rand()% 20 + -10 << "\n";
if(b > 0){
cout << b << " is a positive number." << endl;
}
else if (b == 0){
cout << b << " is equal to zero." << endl;
}
else {
cout << b << " is a negative number." << endl;
}
}
}
Easy enough, first assign the value to b, so you can then compare it.很简单,首先将值赋给 b,然后你就可以比较它了。 The current cout just outputs it.
当前的 cout 只是输出它。
b = rand()% 20 - 10;
cout << "Skaitlis: " << b << "\n";
First of all, your variable b
has no value at all.首先,您的变量
b
根本没有任何价值。 In your loop, you are asking 17 times if b
is greater, lesser or equal to 0, where is your problem.在你的循环中,你问了 17 次
b
是否大于、小于或等于 0,你的问题在哪里。
You have never never assigned to b
.你从来没有分配给
b
。 You will fix it by adding b = rand() % 20 + -10
inside your loop.您将通过在循环中添加
b = rand() % 20 + -10
来修复它。 Now your b
has an actual value.现在你的
b
有一个实际值。
Also, instead of另外,而不是
cout << "Skaitlis: " << rand()% 20 + -10 << "\n";
, ,
change to改成
cout << "Skaitlis: " << b << endl;
. .
Also, your cout
doensn't exist since it's never declared.此外,您的
cout
不存在,因为它从未声明过。 What I want to say is that you don't have cout
function in your program because cout
is found in "iostream" library.我想说的是,您的程序中没有
cout
function,因为cout
位于“iostream”库中。 You have to include "iostream" library as well, with #include <iostream>
at the start of the code.您还必须包含“iostream”库,在代码开头使用
#include <iostream>
。
To clarify:澄清:
Your b
has no value.你的
b
没有价值。 Assign a value to b
before asking if it's greater, lesser or equal to 0;在询问 b 是否大于、小于或等于 0 之前先给
b
赋值;
cout
is not added in your program. cout
未添加到您的程序中。 Include "iostream" with #include <iostream>
使用
#include <iostream>
包含“iostream”
Final code can look like:最终代码可能如下所示:
#include <iostream> // so I can use cout << and cin >>
#include <ctime>
int main() {
int b;
srand(time(NULL)); // NULL is same as 0
for (int a = 0; a < 17; a++) {
b = rand() % 21 - 10; // Assign a value between -10 and +10 to b
if (b > 0) cout << b << "is greater than 0" << endl;
else if (b < 0) cout << b << "is lower than 0" << endl;
else cout << b << "is equal to 0" << endl;
}
return 0;
}
I hope this explanation helps.我希望这个解释有所帮助。
You can use:您可以使用:
#include <random>
void function(){
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(-100, 100); // define the range
int randomNumber = distr(gen);
if(randomNumber > 0)
// bigger than 0
else if(randomNumber == 0)
// equals 0
else
// less than 0
}
I'm not sure if that's what you mean, but you can just assign the result of your random function to a variable, for example:我不确定这是否是你的意思,但你可以将随机 function 的结果分配给一个变量,例如:
int rnd = rand() % 20 - 10;
cout << "Skaitlis: " << rnd << '\n';
if (rnd > 0)
cout << rnd << " is a positive number." << endl;
else if (rnd == 0)
cout << rnd << " is equal to zero." << endl;
else
cout << rnd << " is a negative number." << endl;
Also, if you are just starting programming, I don't think that's necessary to know, but keep in mind that rand()
isn't really a good way to create random numbers.另外,如果您刚刚开始编程,我认为没有必要知道,但请记住
rand()
并不是创建随机数的好方法。 It's way better to use for example mt19937
(although it's a little bit more complicated).最好使用例如
mt19937
(虽然它有点复杂)。 rand()
function generates random numbers only up to 32767 (though it also depends on the compilator you're using) and if you're trying to get a random number in a specific range, there's also a higher chance, that the rand()
function will return a smaller number. rand()
function 最多只能生成 32767 个随机数(尽管它也取决于您使用的编译器),如果您试图获得特定范围内的随机数,那么rand()
function 将返回一个较小的数字。
Consider this sample code:考虑这个示例代码:
#include <iostream>
#include <string>
bool is_positive(int number)
{
if (number >= 0) return true; // Positive number or 0
else return false; // Negative number
}
int main()
{
std::srand(time(0));
for (int i = 0; i < 17; i++)
{
int number = std::rand() % 20 + -10;
if (is_positive(number))
{
std::cout << number << " is positive" << std::endl;
}
else
{
std::cout << number << " is negative" << std::endl;
}
}
}
Here I have defined a function is_positive that returns true if the number is greater than or equal to 0 else it returns false.在这里,我定义了一个 function is_positive,如果数字大于或等于 0,则返回 true,否则返回 false。 I have also shown a simple implementation of the function.
我还展示了 function 的简单实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.