此随机字符串打印代码有什么问题?

时间:2019-09-15 02:52:23

标签: c++ string random

我正在尝试在字符串数组(Alpha)的行中打印X(x =用户输入)数字,如果该行中有“ A”,则应在该行中打印一些内容(字符串或随机数),例如“这行有“ A”“。我无法弄清楚代码中有什么问题,例如代码中的这一行无法正常工作:     (Alpha [dist(gen)] == Alpha [0])

它正在打印具有其他字符串字母(S,P C等)的行中的(“此行具有” A”)

#include <climits>
#include <ctime>
#include <fstream>
#include <iostream>
#include <random>
#include <string>

using namespace std;

int main() {
    int num;
    const int SIZE = 6;

    cout << "How many iterations: ";
    cin >> num;
    string Aplha[SIZE] = {"A", "D", "P", "E", "C", "S"};

    random_device randNum;
    mt19937 gen(randNum());
    uniform_int_distribution<> dist(0, 5);
    uniform_int_distribution<> distRange(INT_MIN, INT_MAX);

    for(int i = 0; i < num; i++) {
        cout << Alpha[dist(gen)];
        if(Alpha[dist(gen)] == Alpha[0]) {
            cout << " this line has A " << distRange(gen);
        }
        cout << endl;
    }

    return 0;
}

结果应该是...

如果一行包含“ A”,则:

A this line has A 123231315
D
P
D
S
A this line has A 845421541
C

我得到的结果是这样的...

How many iterations: 10
S
S this line has A 1950029843
S
C this line has A -145672138
A
C
D
C
E
S

2 个答案:

答案 0 :(得分:0)

您打印的

function getJarak(rWP1,rWP2){ return new Promise(function(resolve,reject){ var myRoute = L.routing.mapbox('thismyapi'); myRoute.route([rWP1, rWP2], function(err, routes) { distance = routes[0].summary.totalDistance / 1000; resolve(distance); }); }); 与检查function tampilTabelJarak(i,nama_kecamatan,distance){ var html; var nomor = i; html += '<tr>'+ '<td>'+nomor+'</td>'+ '<td>'+nama_kecamatan+'</td>'+ '<td>'+distance+'</td>' '</tr>'; $('.data').html(html); 的{​​{1}}不同。每次致电Alpha[dist(gen)]都会产生一个不同的随机数。

答案 1 :(得分:0)

错误在接下来的两行中。每次调用Alpha[dist(gen)]时,都会得到不同的索引。

Alpha[0]

我认为应该像波纹管一样。

dist(gen)