# 做... while循环意外行为(Do… while loop unexpected behavior)

``` 快速解释：我希望将随机数分配给单词，以便每个字母都有不同的数字。 为方便起见，我使用了数组而不是单独的一个字母变量; 这样我就知道“abc [1]”是单词“abc”中的字母'b'。 一个单词的第一个数字使用不同的范围，以避免使用像“075”这样的数字
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int abc[2];
int def[2];

void setRandom()
{
abc[0] = rand() %9+1;
do {abc[1] = rand() %10;} while (abc[1] == abc[0]);
do {abc[2] = rand() %10;} while (abc[2] == abc[1] || abc[2] == abc[0]);

printf("RANDOM abc %d %d %d", abc[0], abc[1], abc[2]); //printed just fine.

do {def[0] = rand() % 9 +1; } while (def[0] == abc[2] || def[0] == abc[1] || def[0] == abc[0]); //code seems to be stuck here
do {def[1] = rand() %10;} while (def[1] == def[0] || def[1] == abc[2] || def[1] == abc[1] || def[1] == abc[0]);
do {def[2] = rand() %10;} while (def[2] == def[1] || def[2] == def[0] || def[2] == abc[2] || def[2] == abc[1] || def[2] == abc[0]);

printf("RANDOM def %d %d %d", def[0], def[1], def[2]); //THIS CODE IS NEVER REACHED. WHY?
}

int main ()
{
setRandom();
printf("RANDOM SET");
}
```
我知道这些数字不是真正随机的。 我知道代码很乱。 我知道这对蛮力数字来说是无效的。 我知道这种设置数字的方式很糟糕，但我想不出更好的事情。 此代码将完成我的工作。
问题是上面的代码在生成def [0]的数字时卡住了; 它将无限生成新的随机数，它似乎完全忽略了“while”循环的一部分。
你可能会建议一个更好的方法来做我正在尝试做的事情，或者只是解决问题的方法，要么会做得很好，因为项目不是很大，它只是用作拼图求解器。 Quick explanation: I want to assign random numbers to words, in a way so every letter has different number. for convenience purposes I have used arrays instead of separate one letter variables; this way I know that "abc[1]" is letter 'b' from word "abc". The first number of a word is using different range to avoid having numbers like "075"
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int abc[2];
int def[2];

void setRandom()
{
abc[0] = rand() %9+1;
do {abc[1] = rand() %10;} while (abc[1] == abc[0]);
do {abc[2] = rand() %10;} while (abc[2] == abc[1] || abc[2] == abc[0]);

printf("RANDOM abc %d %d %d", abc[0], abc[1], abc[2]); //printed just fine.

do {def[0] = rand() % 9 +1; } while (def[0] == abc[2] || def[0] == abc[1] || def[0] == abc[0]); //code seems to be stuck here
do {def[1] = rand() %10;} while (def[1] == def[0] || def[1] == abc[2] || def[1] == abc[1] || def[1] == abc[0]);
do {def[2] = rand() %10;} while (def[2] == def[1] || def[2] == def[0] || def[2] == abc[2] || def[2] == abc[1] || def[2] == abc[0]);

printf("RANDOM def %d %d %d", def[0], def[1], def[2]); //THIS CODE IS NEVER REACHED. WHY?
}

int main ()
{
setRandom();
printf("RANDOM SET");
}
```
I am aware that the numbers are not truly random. I am aware that the code is messy. I am aware it's innefficient to brute-force numbers. I am aware that this way of setting numbers is bad, but I couldn't think of anything better. This code will do the job for my purposes.
The problem is that the above code gets stuck while generating number for def[0]; it will infinitely generate new random numbers, it seems to be completely ignoring 'while' part of the loop.
You may either suggest a better method of doing what I'm trying to do, or simply a solution to the problem, either will do just fine since the project isn't anything big, it's just used as puzzle solver.原文：https://stackoverflow.com/questions/38171138```

## 最满意答案

``` C / C ++中的数组是从零开始的。 创建`abc[2]`只允许0和1合法访问。 超出数组末尾的任何东西都是未定义的行为 - 在这种情况下， `abc[2]` （ 第三个 ，而不是第二个元素）很可能指向`def[0]` 。
修复您的代码，以便两个声明分配3个元素而不是2个。 Arrays in C/C++ are zero-based. creating `abc[2]` give you only 0 and 1 to legitimately access. Anything beyond the end of the array is undefined behaviour - in this case it's probable that `abc[2]` (the third, not second element) points to `def[0]`.
Fix your code so that both declarations allocate 3 elements instead of 2.```
2016-07-04