【问题标题】:How can you get a "x" element from rand()?如何从 rand() 中获取“x”元素?
【发布时间】:2021-09-04 11:01:37
【问题描述】:

我有一个随机数发生器。 现在我的问题是如何获得例如生成的随机数的第一个/第二个/第三个/第四个数字。 实现这一点,以便用户在猜测数字时可以使用提示。

示例:结果(rand):9876

打印提示 1:第二个数字 8

打印提示 2:第四个数字 6

解决这个问题的最佳方法是什么?我一直在考虑将字符串转换为 char 数组,以便打印出保存值的某些位置,但我猜这不会起作用。

如果我问这个问题的方式很大胆,请纠正我。

int nummer = 4; 
std :: string result = "";

for (int i = 0; i < nummer; i++) 
{
    result.push_back(rand()%10 + '0'); 
}

【问题讨论】:

    标签: c++ arrays string random


    【解决方案1】:

    解决这个问题有两种选择:

    1. 09999 之间创建一个随机数,然后计算各个数字。

    2. 09 之间创建四个随机数,代表各个数字,然后,如有必要,从各个数字计算整数。

    通常,执行选项#1 会更直接。您似乎要选择选项#2。这两种方法都是可能的,哪一种更好可能取决于您的程序是更多地使用整个数字还是使用单个数字。

    如果您决定执行选项#2,那么问题就来了09。通常,使用实际数字而不是 ASCII 字符代码会更容易一些,但两种方式都可行。

    就个人而言,我会通过以下方式解决:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        //seed random number generator
        std::srand( std::time(nullptr) );
    
        //note that NUM_DIGITS should not be set so high
        //that MAX_NUMBER is not representable as an `int`
        constexpr int NUM_DIGITS = 4;
    
        //set this to the highest possible number that
        //corresponds to NUM_DIGITS
        constexpr int MAX_NUMBER = 9999;
    
        //this variable holds the entire number
        int random;
    
        //this array holds the individual digits
        int digits[NUM_DIGITS];
    
        ///generate the random number
        random = std::rand()%(MAX_NUMBER+1);
    
        //calculate the individual digits of the random number
        int remaining = random;
        int divisor = (MAX_NUMBER+1) / 10;
        for ( int i = 0; i < NUM_DIGITS; i++ )
        {
            digits[i] = remaining / divisor;
            remaining = remaining % divisor;
            divisor/= 10;
        }
    
        //print the whole random number
        std::cout << "Whole random number:\n" << random << "\n\n";
    
        //print the individual digits
        std::cout << "Individual digits:\n";
        for ( int i = 0; i < NUM_DIGITS; i++ )
        {
            std::cout << digits[i] << "\n";
        }
    }
    

    样本输出(实际输出取决于随机数种子):

    Whole random number:
    8695
    
    Individual digits:
    8
    6
    9
    5
    

    我一直在考虑将字符串转换为 char 数组,但我猜这行不通。

    我认为没有理由这样做。在您发布的代码中,您可以简单地编写

    result[0]
    result[1]
    result[2]
    result[3]
    

    访问号码的各个数字。

    在这方面,将 std::string 转换为 C 风格的字符数组没有任何优势。

    【讨论】:

    • 感谢您的回复,我得到了我现在需要的东西!
    【解决方案2】:

    你写的代码没问题。

    无论您将字符存储在数组还是字符串中,都可以使用result[i] 访问元素,其中i 从0 开始。


    我一直在考虑将字符串转换为 char 数组,但我猜这行不通。

    使用std::string 通常是一个更好的主意 - 仍然可能,但更难搞砸它们,而且它们通常更强大,但如果你愿意,你可以使用 char 数组:

    char result[] = "0000"; // will have null terminator
    
    for (int i = 0; i < 4; ++i)
        result[i] = rand() % 10 + '0';
    

    让结果是一个 5 字符数组 - 带有一个空终止符 - 意味着您仍然可以使用 std::cout &lt;&lt; result 轻松打印所有四位数字。


    或者,您可以选择一个随机的 4 位数字并将其转换为特定宽度的字符串,使用 '0' 填充:

    #include <iostream>
    #include <iomanip>
    
    int main() {
        std::ostringstream oss;
        oss << std::setw(4) << std::setfill('0') << rand() % 10000;
        std::cout << '[' << oss.str() << "]\n";
    }
    

    【讨论】:

    • 这不是我所需要的。但是上面的答案是正确的。
    猜你喜欢
    • 2019-08-13
    • 2020-03-11
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多