このプログラムはランダムな文字列を3回生成します
'step into'を使用してEclipseでデバッグした場合、結果は一意で異なります。
実行したばかりの場合、結果は3回同じ文字列になります
実行方法、デバッグ実行とコンパイル実行によって結果が異なるのはなぜですか?
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <ctime>
#include <cstdlib>
string generaterandomstring(int length){
int i, x, alphabetsize, asciioffset;
string s1;
alphabetsize = 26; // size of all lower case letters
asciioffset = 97; // lower case letters start at 97
srand ( time(NULL) );
for ( i = 0; i < length; i++ )
{
//generate random number
x = rand() % alphabetsize + asciioffset;
cout << "x: " << x;
//get a letter
cout << " char: " << char(x);
//append it to string
s1 = s1 + char(x);
cout << " s1: " << s1 << endl;
}
return s1;
}
int main() {
int i;
string s1;
int length = 3;
srand ( time(NULL) );
for ( i = 0; i < length; i++ )
{
s1 = generaterandomstring(length);
cout << "i is: " << i << " from main s1: " << s1 << endl;
cout << rand() % 10 << endl;
}
cout << "!The End!" << endl; // prints !!!Hello World!!!
return 0;
}