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.