C - Random numbers between 10 and 30
22,520
Solution 1
polje[i]=rand()%21+10;
The %21, gives you a number between 0 and 20. Adding 10, gives you a number between 10 and 30.
Tricky question...Hope that helps...
Solution 2
How about using rand()%20+11? With this you can generate random numbers from 10 to 29. Dint this work? Or try rand()%21+10 This one will be include 30 too.
Solution 3
You can try rand()%21+10;
the answer is in the range of (10,30)
Author by
user2627736
Updated on July 29, 2020Comments
-
user2627736 almost 4 years
I have
polje[i]=rand()%30+1;
for random between 1-30, but how can I make it so it's between 10 and 30? -
Jens Bodal about 8 yearsAnother way to clarify would be that
21 = (30-10)+1
and+10
signifies the starting number. So you could havemin = 10; max = 30; range = max - min; polje[i] = rand() % (range + 1) + min;