-2

I want to create some specific random, such that from 10 to 20.

rand()%20;

is create from 0 to 19

2
  • 1
    So create a random number from 0 to 10 (inclusive) and then add 10 to it. Commented Oct 24, 2015 at 0:15
  • y=mx+c ....................... Commented Oct 24, 2015 at 1:52

1 Answer 1

0

Apply some simple arithmetic

int val = min + (rand() % range);

Where range is (max - min) + 1

A random number betweeen 10-20 is just a random number between 0-10 shifted +10

Sign up to request clarification or add additional context in comments.

9 Comments

Modulo division is not the best idea from the perspective of uniformity of resulting distribution.
@void_ptr True, but this works fine for most uses.
@chux Good catch. Thanks
@JustinMeiners and its this attitude which is why software has so much bugs.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.