<h3>Addressing your problem</h3>
<p>With the expression:</p>
<pre><code>x2 = std::rand() % (x1 - 1) + (x1 - 3); 
</code></pre>
<p>Using your sample input, <code>rand % (x1 - 1)</code> will be <code>rand % 7</code>, this will render you values from <code>0</code> to <code>6</code>, adding <code>5</code> to that you'll have values from <code>5</code> to <code>11</code>.</p>
<p>If you are adamant in using <code>rand</code> you can use something like this:</p>
<pre><code>x2 = rand() / (RAND_MAX / ((x1 - 1) - (x1 - 3) + 1) + 1) + x1 - 3;
</code></pre>
<p><a href="https://godbolt.org/z/WPnh1v" rel="nofollow noreferrer">Live sample</a></p>
<p>The interval is corrected, and this is a less biased method of obtaining the random value than using the modulo, though this problem is never completely solvable when using <code>rand</code>, <strong>it's a known drawback</strong>, there are others, you can find several threads in the website which provide solid reasoning as to why you shouldn't use it, like, for instance:</p>
<p><a href="https://stackoverflow.com/q/52869166/6865932">Why is the use of rand() considered bad?</a></p>
<p><a href="https://stackoverflow.com/q/10984974/6865932">Why do people say there is modulo bias when using a random number generator?</a></p>
<p>You also have this more technical document kindly provided by <a href="https://stackoverflow.com/users/780717/njuffa">njuffa</a>:</p>
<p><a href="https://arxiv.org/pdf/1805.10941.pdf" rel="nofollow noreferrer">Fast Random Integer Generation in an Interval</a></p>
<p>Among many others.</p>
<hr />
<h3>Recommend method</h3>
<p>C++ <a href="https://en.cppreference.com/w/cpp/header/random" rel="nofollow noreferrer"><code>&lt;random&gt;</code></a> header provides better random number generation facilities, for example using <a href="https://en.cppreference.com/w/cpp/numeric/random/random_device" rel="nofollow noreferrer"><code>std::random_device</code></a> and the <a href="https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine" rel="nofollow noreferrer">Mersenne twister engine</a>:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;random&gt;

int main()
{
    int x1;
    int x2;

    std::random_device rand_d; // random number from hardware
    std::mt19937 generator(rand_d()); // seed
    
    while (true)
    {
        std::cin &gt;&gt; x1;
        std::uniform_int_distribution&lt;&gt; uniform_value(x1 - 3, x1 - 1); // setup the range
        x2 = uniform_value(generator); // generante the value
        std::cout &lt;&lt; &quot;result : &quot; &lt;&lt; x2 &lt;&lt; &quot;\n&quot;;
    }
}
</code></pre>
<p><a href="https://godbolt.org/z/xfKfGd" rel="nofollow noreferrer">Live sample</a></p>
Generate a random number depending on input in a defined interval
<p>I want to generate random numbers that deppend on input from user.</p>
<p>For example if user inputs <code>8</code>, I want to generate number from <code>5</code> to <code>7</code>,
<code>(8 - 3)</code> to <code>(8 - 1)</code></p>
<p>My code:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;time.h&gt;

int main()
{

    int x1;
    int x2;

    std::srand(time(0));

    while (true)
    {
        std::cout &lt;&lt; &quot;input : &quot;;
        std::cin &gt;&gt; x1;

        x2 = std::rand() % (x1 - 1) + (x1 - 3);

        std::cout &lt;&lt; &quot;result : &quot;;
        std::cout &lt;&lt; x2 &lt;&lt; std::endl;
    }
    return 0;
}
</code></pre>
<p>But the output is:</p>
<pre><code>input : 8
result : 7
input : 8
result : 10
input : 8
result : 8
</code></pre>
<p>And I want <code>result</code> to be from <code>5</code> to <code>7</code>.</p>