How to set the range for generating random numbers in C using rand() function? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 4
Oh ok Kishalaya Saha, Thanks for answering / explaining, much appreciated : )
2nd Aug 2018, 12:26 PM
Ipang
+ 1
Hi 怌ļ¼Øļ¼”ļ¼°ļ¼°ļ¼¹ ļ¼“ļ¼Æ ļ¼Øļ¼„ļ¼¬ļ¼°ć€ 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 怌ļ¼Øļ¼”ļ¼°ļ¼°ļ¼¹ ļ¼“ļ¼Æ ļ¼Øļ¼„ļ¼¬ļ¼°ć€ 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