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

Time:01-02

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)

CodePudding user response:

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;
  • Related