簡體   English   中英

我該如何在C ++中生成一個隨機數?

[英]How do I generate any random number UNDER or ABOVE a certain number in C++?

例如,我正在做一個猜謎游戲。 如果計算機猜得太低,我想將其發送給此功能

int player1::guessLow(int g)
{
return rand() % guess + 1;
}

這樣它就可以猜測上面所猜測的任何數字。 我也想對過高的地方做同樣的事情

int player1::guessHigh(int g)
    {
    return rand() % guess - 1;
    }

顯然這不是正確的代碼,但是我該怎么做? <和>運算符在猜測之間不起作用。 我正在嘗試提供任何隨機數,以幫助計算機記住該數字,以便它不斷猜測該數字以下還是以上。 我將如何完成? 有沒有可以使用的算法或模板?

更新:

下面是代碼

   bool checkForWin(int guess, int answer) 
{ 
    cout << "You guessed " << guess << ". ";
    if (answer == guess) 
    { 
        cout << "You're right! You win!" << endl; 
        return true; 
    } 
    else if (answer < guess) 
        cout << "Your guess is too high." << endl; 
    else 
        cout << "Your guess is too low." << endl; 
    return false; 
} 

void play(Player &player1, Player &player2) 
{ 
    int answer = 0, guess = 0; 
    answer = rand() % 100;
    bool win = false; 

    while (!win) 
    { 
    cout << "Player 1's turn to guess." << endl; 
    guess = player1.getGuess(); 
    win = checkForWin(guess, answer); 
    if (win) return; 

    cout << "Player 2's turn to guess." << endl; 
    guess = player2.getGuess(); 
    win = checkForWin(guess, answer); 
    }
}

rand()是C ++的一部分(至少現在是這樣),對於簡單的猜謎游戲也很合適。

在某些情況下, srand可能不合適,例如,出於測試目的,我可能想要重復的行為和可預測性。

對於此問題,您可能希望使用srand否則猜謎游戲將變得無聊。

您應該完全避免在許多應用程序中使用rand ,例如密碼學。

但是,這里的問題更為基本。 您無需跟蹤所有猜中的數字。 您只需要跟蹤最小和最大范圍即可。 例:

#include <iostream>
#include <ctime>
#include <cstdlib>

using std::cout;

int main()
{
    srand((unsigned int)time(NULL));
    int guess = rand();
    int min = 0;
    int max = RAND_MAX;
    while(true)
    {
        int n = min + rand() % (max - min);
        //(n goes up to max, not including max)

        if(n < guess)
        {
            min = n + 1;
            cout << n << " too low\n";
        }
        else if (n > guess)
        {
            max = n;
            cout << n << " too high\n";
        }

        if(min == max)
            n = min;

        if(n == guess)
        {
            cout << n << " success\n";
            break;
        }
    }
    return 0;
}

或使用注釋中建議的此功能來查找范圍內的數字。

int rand_rang(int min, int max)
{
    if(min == max)
        return min;
    return min + (int)((double)rand() / ((double)RAND_MAX + 1) * (max - min));
}

這些都是偽隨機數。 如果您是為彩票公司設計此游戲的,請使用更難破解的安全隨機數生成器。 如果分布非常重要(物理模擬等),那么您又要避免rand

有許多使用標准C ++工具生成給定范圍內的隨機數的示例。 這是一個小線程安全功能,可為您提供一定范圍內的數字:

#include <chrono>
#include <iostream>
#include <random>

long random( long min, long max )
{
  // Create and initialize our PRNG
  thread_local auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  thread_local std::ranlux48 prng( seed );
  return std::uniform_int_distribution <long> ( min, max )( prng );
}

(如果只有單線程,則可以用static替換thread_local 。)

要獲取僅受最小值或最大值限制的范圍,請使用numeric_limits<>查找要與之綁定的最低/最高值:

#include <limits>

int main()
{
  std::cout << "Maximum value of 12: " << random( std::numeric_limits <long> ::min(), 12 ) << "\n";
}

希望這可以幫助。

暫無
暫無

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

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