Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading