How can I generate random integers between 0 and 9 using Python?

In this article, we will show you how to generate random integers between 0 and 9 in Python. Below are the various methods to accomplish this task:

  • Using randint() method

  • Using randrange() method

  • Using secrets module

To generate random integers within certain ranges, Python supports a variety of functions. We will discuss Python's built-in random module for generating random numbers.

The two primary functions provided by the random module to produce random integers are randint() and randrange(). In order to produce random values, a second module called secrets is also used.

Using randint() method

The random.randint() method is used to return a random integer within the specified range.

NOTE

Float numbers cannot be used in the randint() method. If you enter values other than integers, a ValueError: non-integer arg 1 error will be raised.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -.

  • Use the import keyword to import the random module.

  • Use the randint() function of the random module to generate a random integer between 0 and 9.

  • Print the generated random integer between 0 and 9.

Example

The following program generates a random integer between 0 and 9 using random.randint() method -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing random module</span>
<span class="token keyword">import</span> random
<span class="token comment"># generating a random integer between 0 and 9 without using a loop</span>
randomNumber <span class="token operator">=</span> random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span>
<span class="token comment"># printing the random integer between 0 and 9</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The generated random integer between 0 and 9 = "</span><span class="token punctuation">,</span> randomNumber<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The generated random integer between 0 and 9 = 4

Generating some random numbers between 0 and 9 using randint() with for loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -.

  • Use the import keyword to import the random module.

  • Use the for loop to traverse in the specified range using the range() function(returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number). Here we are running the loop 5 times.

  • Use the randint() function of the random module to generate a random integer between 0 and 9.

  • Print the generated 5 random integers between 0 and 9.

Example

The following program generates some random integers between 0 and 9 using random.randint() method with the for loop -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing random module</span>
<span class="token keyword">import</span> random
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 5 generated random integers between 0 and 9 using for loop: "</span><span class="token punctuation">)</span>
<span class="token comment"># traversing in the range from 0 to 5(excluded)</span>
<span class="token comment"># (running the loop 5 times)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># generating a random integer between 0 and 9</span>
   randomNumber <span class="token operator">=</span> random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span>
   <span class="token comment"># printing the generated random integer</span>
   <span class="token keyword">print</span><span class="token punctuation">(</span>randomNumber<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The 5 generated random integers between 0 and 9 using for loop:
7
2
4
8
5

Using randrange() method

The random.randrange() method is used to return a random integer within the specified range.

Syntax

random.randrange(start, stop, step)

Parameters

  • start(optional) - an integer representing the starting position. 0 is the default.

  • stop(required) - an integer representing the ending position

  • step(optional) - an integer representing the incrementation. 1 is the default.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -.

  • Use the import keyword to import the random module.

  • Use the randrange() function of the random module to generate a random integer between 0 and 9 by passing only the stop argument.

  • Print the generated random integer between 0 and 9.

Example

The following program generates a single random integer between 0 and 9 using random.randrange() method -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing random module</span>
<span class="token keyword">import</span> random
<span class="token comment"># generating a random integer between 0 and 9 without using loop</span>
<span class="token comment"># we are only passing the stop value here</span>
randomNumber <span class="token operator">=</span> random<span class="token punctuation">.</span>randrange<span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span>
<span class="token comment"># printing the random integer between 0 and 9</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The generated random integer between 0 and 9 = "</span><span class="token punctuation">,</span> randomNumber<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The generated random integer between 0 and 9 = 2

Generating some random numbers between 0 and 9 using randrange() with for loop

Example

The following program generates some random integers between 0 and 9 using random.randrange() method with for loop -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing random module</span>
<span class="token keyword">import</span> random
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 5 generated random integers between 0 and 9 using for loop: "</span><span class="token punctuation">)</span>
<span class="token comment"># traversing in the range from 0 to 5(excuded)</span>
<span class="token comment"># (running the loop 5 times)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># generating random integer between 0 and 9</span>
   <span class="token comment"># we are passing the start, stop parameters here</span>
   randomNumber <span class="token operator">=</span> random<span class="token punctuation">.</span>randrange<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span>
   <span class="token comment"># printing the generated random integer</span>
   <span class="token keyword">print</span><span class="token punctuation">(</span>randomNumber<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The 5 generated random integers between 0 and 9 using for loop:
6
1
7
2
2

Using secrets module

To generate random integers, we can use the randbelow() function found in the secrets module. It generates random numbers with high cryptographic security.

In Python 3.6, the secrets module is a new one. For purposes involving security or cryptography, this is preferable than the random module.

Example

The following program generates 5 random integers between 0 and 9 using randbelow() function with the for loop -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># importing randbelow function from secrets module</span>
<span class="token keyword">from</span> secrets <span class="token keyword">import</span> randbelow
<span class="token comment"># traversing in the range from 0 to 5(excluded) using for loop</span>
<span class="token comment"># (running the loop 5 times)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># generating a random integer between 0 and 9 using randbelow() function</span>
   <span class="token keyword">print</span><span class="token punctuation">(</span>randbelow<span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

0
5
4
4
8

Conclusion

We learned about two different modules for generating random integers between 0 and 9. To generate random integers between 0 and 9, we used two functions from Python's random module, randint() and randrange(). We also looked at the new secrets module, which was introduced in Python 3.x. We also talked about these functions with and without a loop.

Updated on: 2022-10-28T11:23:17+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements