2013-10-07 44 views
0

我是新來的c + +,我不明白爲什麼數字超出範圍。我
評論 ,我還以爲你最初的範圍爲什麼我在超出範圍之外獲得數字?

#include <vector> 
#include <iostream> 
#include <random> 
#include <time.h> 

using namespace std; 
using std::vector; 

int main() 
{ 

vector<int> deck; 
default_random_engine eng(time(0)); 

我還以爲這裏是哪裏我指定的隨機數的範圍,而是我得到這個範圍

uniform_int_distribution<int> dis(0, 51); 

int pos1; 
int pos2; 
int num1; 
int num2; 
int i; 
int n; 
int m; 

for (i = 1; i < 53; i++) 
{ 
    deck.push_back(i); 

} 

pos1 = dis(eng); 
pos2 = dis(eng); 

num1 = deck.at(pos1); 
num2 = deck.at(pos2); 


for (n = 0; n < 100; n++) 
{ 
    pos1 = dis(eng); 
    pos2 = dis(eng); 

    cout << pos1 << "\n" << pos2; 

} 




} 

回答

8

它看起來外面 號像問題是在最後一行cout << pos1 << "\n" << pos2;(這裏你忘了添加<< "\n")。所以,如果你的程序打印四個數字(例如22,23,24,25),你會看到下面的文字:

22 
2324 
25 

所以數2324將是你的範圍之外,但實際上它是兩個數的級聯。

+0

Waoh,趕上! – Simon

相關問題