0

Random number

Can anyone tell that how to find a random number between 1 to 10 IMPORTANT - PLEASE DEFINE THE LOGIC BEHIND IT

12th Jun 2018, 1:07 PM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar
5 odpowiedzi
+ 2
Aveek Bhattacharyya You need to _apply_ what I have posted to the code posted by estifanos tekiea and what you have already learned. Neither of us is here to do your work for you. Hint: You rand() will produce predictable numbers without a seed. We generally use the time as a seed. Look at the srand() function.
13th Jun 2018, 3:15 PM
non
+ 1
1. Get a random number. 2. Use % (modulus) 9 because modulus gives the remainder for division by the right operand, eg: 5 % 4 will give 1 because 5/4 = 1 remainder 1. 3. Add 1 to our final result. This prevents 0, eg: 5 % 5. Note we used 9 because we're adding 1. You already have an example code and your brains so I'll give you just a piece of code and you apply it: int ii = rand() % 9; ii++; // Can be written as (better): int ii = 1 + (rand() % 9); That should suffice. You have all the weapons you need, now fight!
12th Jun 2018, 5:24 PM
non
12th Jun 2018, 2:47 PM
estifanos
estifanos - avatar
0
using randi() or srandi() function
14th Jun 2018, 9:24 AM
Brajesh Kumar
Brajesh Kumar - avatar
- 2
It's always giving the same value
13th Jun 2018, 11:55 AM
Aveek Bhattacharyya
Aveek Bhattacharyya - avatar