Warning: session_start(): open(/tmp/sess_c9b2e2b294652f314d5af0e9a811ff23, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Generate N Unique Random Numbers Within A Range In Python - LearnShareIT

How To Generate N Unique Random Numbers Within A Range In Python

To generate N unique random numbers within a range in Python, you can use the random.sample(), random.shuffle(), or random.randint() functions. Let’s learn how to use them.

Generate N Unique Random Numbers Within A Range In Python

Using the random.sample() function

You can use the random.sample() function to generate N unique random numbers within a range in Python. But note that, you have to assign N as less or equal to the number of elements in a range. If not, you will get an Error.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
    # Create a range.
    my_range = range(start, end)

    # Generate N Unique Random Numbers Within A Range with the random.sample() function.
    result = random.sample(my_range, n)
    print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[5, 2, 1, 6, 7, 9, 4]
[21, 24, 57, 65, 49, 32, 64]

Using the random.shuffle() function

Besides the random.sample() function, you can use the random.shuffle() function to generate N unique random numbers within a range. Unlike the random.sample() function, we will random a list of a range first. Then, we will get the first n elements of the new list.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
    # Create a range.
    my_range = range(start, end)

    # Generate N Unique Random Numbers Within A Range with the random.sample() function.
    result = random.sample(my_range, n)
    print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[9, 2, 3, 8, 6, 1, 4]
[81, 26, 44, 62, 97, 29, 14]

Using the random.randint() function

In addition, you can use the random.randint() function combined with the set data structure to generate N unique random numbers within a range. We will use the random.randint() function until the size of the set is N.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
    # Create a range.
    my_range = range(start, end)

    # Generate N Unique Random Numbers Within A Range with the random.sample() function.
    result = random.sample(my_range, n)
    print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[1, 4, 5, 6, 7, 9, 10]
[65, 35, 38, 75, 76, 13, 95]

Besides, you can learn how to generate random numbers in the range excluding some numbers here.

Summary

We have shown you how to generate N unique random numbers within a range in Python in 3 ways. Personally, you should use the random.sample() or random.shuffle() functions because they have higher performance than the random.randint() function in this case. Leave your comment below if you have any questions. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *