该程序生成随机字符串 3 次
当在 Eclipse 中使用“step into”进行调试时,结果是唯一且不同的
刚执行时结果是相同的字符串 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;
}