0
Random number
Can anyone tell that how to find a random number between 1 to 10 IMPORTANT - PLEASE DEFINE THE LOGIC BEHIND IT
5 Answers
+ 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.
+ 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!
0
using randi() or srandi() function
- 2
It's always giving the same value