简体   繁体   English

如何获得我的代码以随机打印字符或字符串?

[英]How can i get my code to print a character or string randomly?

So I'm trying to test something out to see how rand() works in c++! 因此,我正在尝试测试一些东西,以查看rand()在c ++中的工作方式! I'm very new to c++ but I'm wondering how can I get the following code to print out either the letter B or A when the number 1 or 2 is generated?? 我是C ++的新手,但是我想知道如何在生成数字1或2时获取以下代码以打印出字母B或A? the code ask if he wants to generate a letter, enter y for yes or n for no! 代码询问他是否要生成字母,请输入y(是)或n(否)! then a letter generates but it seems to only generate the letter A?? 然后生成一个字母,但似乎只生成字母A? any idea? 任何想法?

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

    using namespace std;

    int main(){
        char x;
        char d;
        cout << "generate random letter?(y/n)" << endl;
        cin >> x;
        if (x = 'y'){
    srand(time(NULL));

    int randNum = (rand() % 2) + 1;
    if (randNum = 1){
        d = 'A';
    }
    else {
        d = 'B';
    }
    cout << d << endl;
}
else{ return 0; }
system("pause");
    }

Change this: 更改此:

if (randNum == 1){ 如果(randNum == 1){

In modern C++, rand() is deprecated , and other techniques based on eg mt19937 Mersenne twister engine and uniform_int_distrubtion are used. 在现代C ++, rand()已过时 ,和基于例如其它技术mt19937梅森捻线机发动机和uniform_int_distrubtion被使用。

You may want to consider some code like this: 您可能需要考虑以下代码:

#include <iostream>     // For console output
#include <random>       // For pseudo-random number generators and distributions

int main()
{
    // Use random_device to generate a seed for Mersenne twister engine.
    std::random_device rd;    

    // Use Mersenne twister engine to generate pseudo-random numbers.
    std::mt19937 engine(rd());

    // "Filter" MT engine's output to generate pseudo-random integer values,
    // **uniformly distributed** on the closed interval [0, 1].  
    // (Note that the range is [inclusive, inclusive].)
    std::uniform_int_distribution<int> dist(0, 1);

    // Print 20 pseudo-random letters (A, B).
    for (int i = 0; i < 20; ++i)
    {
        // Generate pseudo-random number
        if (dist(engine) == 0)
        {
            std::cout << 'A';
        }
        else 
        {
            std::cout << 'B';
        }
        std::cout << ' ';
    }
    std::cout << std::endl;
}

When you say: 当你说:

if(randNum = 1)

you assign the value 1 to randNum , then check if it is True . 您将值1分配给randNum ,然后检查它是否为True In C/C++ any non-zero number is True . 在C / C ++中,任何非零数字均为True In this way you always enter the first part of the if statement and get an 'A'. 这样,您总是输入if语句的第一部分并得到一个“ A”。

Instead if you say: 相反,如果您说:

if(randNum == 1)

you are comparing randNum to 1 and will get the behavior you want. 您将randNum1进行比较,将得到您想要的行为。

There's a much easier way to generate a random char. 有一种更简单的方法来生成随机字符。 Chars can be created from an ASCII code number since we know from uppercase A to lowercase Z is in the range of 65-122 (google ASCII table) 可以从ASCII码编号创建字符,因为我们知道从大写A到小写Z的范围是65-122(Google ASCII表)

std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<int> dist(65, 122);
char ascii_char = dist(engine);
std::cout << scii_char;

as a side note, std::uniform_int_distribution dist(65, 122) is inclusive, so it includes both 65 and 122. 作为附带说明,std :: uniform_int_distribution dist(65,122)是包含端点的,因此它包括65和122。

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

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