need to generate a random number, but every digit in that number can't be higher than 6 (in C)

49 views Asked by At

I need to generate a random number that has 4 digits, but I have to make sure that every digit is not higher than 6, Is it possible? if so, how can I do that? :) (In C)

1

There are 1 answers

3
lulle2007200 On BEST ANSWER

Simplest option would be to generate 4 different random numbers in the range of 0 to 6, then add them up, but multiply the first one by 1, second one by 10 etc.

int a = rand() % 7;
int b = rand() % 7;
int c = rand() % 7;
int d = rand() % 7;

int e = a + 10 * b + 100 * c + 1000 * d;