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?
Asked
Active
Viewed 2.5k times
2

MOHAMED
- 41,599
- 58
- 163
- 268

user2627736
- 281
- 1
- 4
- 13
-
possible duplicate of [How to generate a random number from within a range - C](http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range-c) and of [Generate random numbers uniformly over entire range](http://stackoverflow.com/questions/288739/generate-random-numbers-uniformly-over-entire-range) – Jul 28 '13 at 14:34
-
little late but the simplest answer is int random = arc4random_uniform(20) + 10; – Sam B Apr 03 '16 at 15:42
3 Answers
5
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...

Samir Seetal
- 392
- 6
- 8
-
1Another way to clarify would be that `21 = (30-10)+1` and `+10` signifies the starting number. So you could have `min = 10; max = 30; range = max - min; polje[i] = rand() % (range + 1) + min;` – Jens Bodal Apr 30 '16 at 18:32
1
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.

Bhargav Ponnapalli
- 9,224
- 7
- 36
- 45