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

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

int abc;
int def;

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

printf("RANDOM abc %d %d %d", abc, abc, abc); //printed just fine.

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

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

int main ()
{
setRandom();
printf("RANDOM SET");
}
```
我知道这些数字不是真正随机的。 我知道代码很乱。 我知道这对蛮力数字来说是无效的。 我知道这种设置数字的方式很糟糕，但我想不出更好的事情。 此代码将完成我的工作。
问题是上面的代码在生成def 的数字时卡住了; 它将无限生成新的随机数，它似乎完全忽略了“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" 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;
int def;

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

printf("RANDOM abc %d %d %d", abc, abc, abc); //printed just fine.

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

printf("RANDOM def %d %d %d", def, def, def); //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; 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`只允许0和1合法访问。 超出数组末尾的任何东西都是未定义的行为 - 在这种情况下， `abc` （ 第三个 ，而不是第二个元素）很可能指向`def` 。
修复您的代码，以便两个声明分配3个元素而不是2个。 Arrays in C/C++ are zero-based. creating `abc` 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` (the third, not second element) points to `def`.
Fix your code so that both declarations allocate 3 elements instead of 2.```
2016-07-04