簡體   English   中英

c++ 測試隨機數是正數還是負數

[英]c++ test if random number is positive or negative

我有 C++ 的學校作業,我是新來的。 我必須生成 17 個隨機數,然后測試它們是否為正/負/等於零。 我似乎無法找到一種方法來從 for 循環中獲取隨機生成的數字以在 if/else if 循環中對其進行測試。 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;
        }
   }
}

很簡單,首先將值賦給 b,然后你就可以比較它了。 當前的 cout 只是輸出它。

b = rand()% 20 - 10;
cout << "Skaitlis: " << b << "\n";

首先,您的變量b根本沒有任何價值。 在你的循環中,你問了 17 次b是否大於、小於或等於 0,你的問題在哪里。

你從來沒有分配給b 您將通過在循環中添加b = rand() % 20 + -10來修復它。 現在你的b有一個實際值。

另外,而不是

cout << "Skaitlis: " << rand()% 20 + -10 << "\n"; ,

改成

cout << "Skaitlis: " << b << endl; .

此外,您的cout不存在,因為它從未聲明過。 我想說的是,您的程序中沒有cout function,因為cout位於“iostream”庫中。 您還必須包含“iostream”庫,在代碼開頭使用#include <iostream>

澄清:

你的b沒有價值。 在詢問 b 是否大於、小於或等於 0 之前先給b賦值;

cout未添加到您的程序中。 使用#include <iostream>包含“iostream”

最終代碼可能如下所示:

#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;
}

我希望這個解釋有所幫助。

您可以使用:

#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
}

我不確定這是否是你的意思,但你可以將隨機 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;

另外,如果您剛剛開始編程,我認為沒有必要知道,但請記住rand()並不是創建隨機數的好方法。 最好使用例如mt19937 (雖然它有點復雜)。 rand() function 最多只能生成 32767 個隨機數(盡管它也取決於您使用的編譯器),如果您試圖獲得特定范圍內的隨機數,那么rand() function 將返回一個較小的數字。

考慮這個示例代碼:

#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;
        }
    }
}

在這里,我定義了一個 function is_positive,如果數字大於或等於 0,則返回 true,否則返回 false。 我還展示了 function 的簡單實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM