我试图做一个程序产生的随机数,直到找到一组预定义的数字(例如,如果我有一组我5个喜爱的数字,多少次,我需要打的让电脑随机找到相同的号码)。我已经写了一个简单的程序,但不明白这似乎是稍稍无关我所期望的结果,例如结局不一定包含所有预定义的数字有时它(甚至不停止循环从运行)。我认为,问题出在逻辑运算符“& &”但我不知道。这里是代码:C++的随机数的逻辑运算符奇怪的结果
const int one = 1;
const int two = 2;
const int three = 3;
using namespace std;
int main()
{
int first, second, third;
int i = 0;
time_t seconds;
time(&seconds);
srand ((unsigned int) seconds);
do
{
first = rand() % 10 + 1;
second = rand() % 10 + 1;
third = rand() % 10 + 1;
i++;
cout << first<<","<<second<<","<<third<< endl;
cout <<i<<endl;
} while (first != one && second != two && third != three);
return 0;
}
,这里是出可能的结果:
3,10,4
1 // itineration variable
7,10,4
2
4,4,6
3
3,5,6
4
7,1,8
5
5,4,2
6
2,5,7
7
2,4,7
8
8,4,9
9
7,4,4
10
8,6,5
11
3,2,7
12
我也注意到,如果我使用||操盘& &的循环将执行,直到找到确切的数字尊重其中的变量设置(在这里:1,2,3)的顺序。这是更好的,但是我应该怎么做循环停止,即使订单不一样,只有数字?感谢您的回答和帮助。
请编辑这个问题,包括你有更新试图发布为[新问题](http://stackoverflow.com/questions/12182551/negation-before-brackets)。 –