简体   繁体   English

如何生成比之前生成的随机数更高或更低的随机数?

[英]How can I generate a random number which is, higher or lower than the random number generated before?

/*Instead of increment 1 or decrease 1 the random number in the for loop, I would like the code to generate a random integer higher than the previous random generated if guess is higher and to generate a random integer lower than the previous random generated if the guess is lower./ / *不是让for循环中的随机数增加1或减少1,我希望代码生成一个比先前的随机数高的随机整数(如果猜测值较高),并生成一个比先前的随机数低的随机整数,如果猜测更低。/

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

using namespace std;

int main(){
    int counter = 0;
    unsigned int guess;
    srand(time(NULL));
    unsigned int random_number = (rand() % 100)+ 1;

    std::string higher = "higher";
    std::string lower = "lower";
    std::string value = "";

    cout << "Please enter a number between 1 and 100, so the computer can guess it!" << endl;
    cin >> guess;
    cout << random_number << endl;

    while (guess != random_number)
    {
        cout << "Is it a lower or higher value?" << endl;
        cin >> value;
        if (value == higher)
        {
            for(int i = 1; i< 2; i++)
            {
                random_number = random_number + 1;
                cout << random_number << endl;
            }
        }
        else if (value == lower)
        {
            for(int i =1; i < 2; i++)
            {
                random_number = random_number - 1;
                cout << random_number << endl;
            }
        }
    }
    cout << "This PC is fabulous" << endl;

    return 0;
}

You just need to call random with your new limits. 您只需要使用新的限制来随机调用即可。

To make things easier let us write a function that generates integers between any two limits we specify, say low_limit and low_limit . 为了使事情变得更容易,让我们编写一个函数,该函数在我们指定的任意两个限制(例如low_limitlow_limit之间生成整数。

int random(int low_limit, int up_limit){
    return rand() % ((up_limit - low_limit) + 1) + low_limit;
}

Now generate the first number by calling the function and save the result into a temp variable. 现在,通过调用函数生成第一个数字,并将结果保存到temp变量中。

temp = random (0, 100);

Now upon user input you can use temp to call random like this: 现在,在用户输入后,您可以使用temp来像这样调用random

   if (value == higher)
       temp = random (temp, 100);
    else if (value == lower)
       temp = random (0, temp);

For different random numbers each run you can use srand function with a parameter that can give a different seed each time like system time. 对于每次运行的不同随机数,您可以使用带有参数的srand函数,该参数可以每次提供不同的种子,例如系统时间。

srand(time(NULL));

note: Use the proper headers. 注意:使用正确的标题。

UPDATE 更新

Based on your comments, you can use extra two variables say: minguess and maxguess to narrow your random generation range. 根据您的评论,您可以使用另外两个变量,例如: minguessmaxguess来缩小您的随机生成范围。 check the following piece of code: 检查以下代码:

srand(time(NULL));
int choice, minguess = 0, maxguess = 100, current;
do{
    choice = 0;
    current = random(minguess,maxguess);
    cout << "\n My guess is: " << current << "\n";
    while (choice < 1 || choice > 3){
        cout << "\n Enter you choice: \n";
        cout << " 1- higher. \n";
        cout << " 2- lower. \n";
        cout << " 3- quit. \n\n";
        cin >> choice;
    }
    if (choice == 1)
        minguess = current;
    else if (choice == 2)
        maxguess = current;
} while (choice != 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM