How to set the range for generating random numbers in C using rand() function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to set the range for generating random numbers in C using rand() function?

2nd Aug 2018, 5:45 AM
Yash 💎
Yash 💎 - avatar
4 Answers
+ 4
Oh ok Kishalaya Saha, Thanks for answering / explaining, much appreciated : )
2nd Aug 2018, 12:26 PM
Ipang
+ 1
Hi 「HAPPY TO HELP」 the second ex shows 'rand() % 50 + 1' and the third is 'rand() % 51' these looks the same to me, can you help me understand how they yield different output? Thanks : )
2nd Aug 2018, 8:24 AM
Ipang
+ 1
Ipang, it's the lower limit of the number that is different, which 「HAPPY TO HELP」 called the MINIMUM. The third one starts from 0, while the second one starts from 1. The remainder modulo 50 is a number between 0 and 49 (both inclusive). Add 1 to that, and you get numbers between 1 (=0+1) and 50(=49+1).
2nd Aug 2018, 9:56 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
Happy to help 😉
2nd Aug 2018, 3:36 PM
Kishalaya Saha
Kishalaya Saha - avatar