Output random number between 30-35 using Random.seed(), 'for' and multiplication in Python
<p>I am new to programming. I had an assignment to write a code that will output a random number between 30 and 35. This code needs to use <code>random.seed()</code>, a FOR statement and a multiplication. I understand the <code>random.seed([x])</code> generates an initial value that could be used in the proceeding section of the code. However, I cant figure out how to proceed after obtaining the value of the random:</p>

<pre><code>import random
random.seed(70)
print(random.random()) # This returns a value of 0.909769237923872
</code></pre>

<p>How do i use this value to generate a random value between 30 and 35?</p>

<p>Note: Without the specific directions above, I have been able to write two codes that functions as desired, so please I am not looking for alternative ways of writing the code.  </p>

<p>I'm not exactly sure how the <code>for</code> loop is relevant here, but you can use the formula that follows (based off Java's random number in range equation, mentioned <a href="http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range">here</a>) and in particular <a href="http://stackoverflow.com/a/363732/5647260">this</a> answer, which is as follows:</p>

<pre><code>minimum + int(random.random() * (maximum - minimum))
</code></pre>

<p>The above will generate a number in the interval <code>[minimum, maximum)</code>. This is because multiplying the random number in <code>[0, 1)</code> by <code>maximum - minimum</code> will give you a range of values, from <code>maximum</code> (inclusive) to <code>maximum</code> exclusive. Then you just add the <code>minimum</code> to that range to achieve the range of random values.</p>

<p>We can nerf it to fit your needs of <code>[30, 35]</code> by setting minimum to 31 (inclusive) and 35 (exclusive):</p>

<pre><code>31 + int(random.random() * (35 - 31))
</code></pre>

<p>The above will generate <code>(30, 35)</code>, or a number between 30 and 35 exclusive:</p>

<pre><code>&gt;&gt;&gt; print(31 + int(random.random() * (35 - 31)))
34
&gt;&gt;&gt; print(31 + int(random.random() * (35 - 31)))
32
&gt;&gt;&gt; print(31 + int(random.random() * (35 - 31)))
32
...
</code></pre>

<p>This would most optimally be applied into a function, like so:</p>

<pre><code>def random_in_range(minimum, maximum): #exclusive to exclusive
    return (minimum + 1) + int(random.random() * (maximum - (minimum + 1)))
</code></pre>

<p>And called like:</p>

<pre><code>&gt;&gt;&gt; print(random_in_range(30, 35))
33 
</code></pre>

python python-3.x random