Python | Generate a random number in the range of 1 to 10 using the function

Method 1: Using randint() from NumPy

import numpy 
def print_rand_num(start, end):
	    
    return numpy.random.randint(start, end)

start = 1
end = 10
print("Your unique number in b/w the range of 1 to 10 is:", print_rand_num(start, end))

Output:

Your unique number in b/w the range of 1 to 10 is: 7

Explanation:

In this method, we used the numpy library. We defined two variables start and end and passed those to the function named print_rand_num. Now once passed we used the randint() function that takes two parameters i.e the start and end number in between whose we want to generate the integer.

Method 2: Using random.randint()

import random
def gen_random_num(start, end):
	    
    return random.randint(start, end)

start = 1
end = 11
print("Your unique number in b/w the range of 1 to 10 is:", gen_random_num(start, end))

Explanation:

Same as NumPy but here we used the function of a random library

Method 3: Using randbelow() function

from secrets import randbelow

result =  randbelow(11)
print("Your unique number in b/w the range of 1 to 10 is:", result)

Method 3: Using the randbelow() function

import random
def gen_random_num(start, end):
	    
    return int(random.uniform(start, end))

start = 1
end = 11
print("Your unique number in b/w the range of 1 to 10 is:", gen_random_num(start, end))

Method 4: Using uniform() and int

import random
def Rand(start, end):
	    
    return int(random.uniform(start, end))

start = 1
end = 11
print("Your unique number in b/w the range of 1 to 10 is:", Rand(start, end))

Explanation:

Here in this method we used a random library but the function we used is different from randint(). The one we used is uniform(). This takes the start and end digits and provides a float value. We used the int data type to convert it to an integer value.

Method 5: Using the randrange()

import random

result = random.randrange(11)
print("Your unique number in b/w the range of 1 to 10 is:", result )

Explanation

Using randrange() we generated a number. We simply have to pass an argument that represents the range.

Share:

Author: Dhvanil V