• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Random Number Without Repeating in Python

Random Number Without Repeating in Python

June 3, 2022 4 Comments

To generate random numbers without repeating in Python, you can use the random module function choices(). choices() takes a list and the number of random numbers you want to generate.

import random

lst = range(0,100)

print(random.choices(lst, k=10))

#Output:
[37, 95, 88, 82, 15, 38, 60, 71, 56, 49]

When working with data, it can be very useful to generate random numbers to be able to perform simulations or get a random sample of a dataset.

In Python, we can generate random numbers in a range easily. The Python random module has many useful functions for generating random numbers.

The generation of random numbers is pretty easy, but sometimes we need to generate random numbers and have all of our numbers be unique.

In Python, you can easily generate random numbers without repeating.

To generate random numbers without repeating in Python, you can use the random module function choices(). choices() takes a list and the number of random numbers you want to generate.

Below is a simple example of how to generate random numbers between 0 and 100 without repeating in Python.

import random

lst = range(0,100)

print(random.choices(lst, k=10))

#Output:
[37, 95, 88, 82, 15, 38, 60, 71, 56, 49]

Generating Random Numbers from a List of Numbers Without Repeating in Python

If you have a list of numbers that you want to get random numbers from without repeating, there is an extra step you need to take.

First, if you have a list of numbers, we need to remove duplicates so that we can use choices().

To remove the duplicates from your list, you should convert it to a set with set(). Then, you can convert it back to a list and use choices().

Below is an example of how to generate non-repeating random numbers with Python from a list of numbers with duplicates.

import random

lst = [0,0,1,2,2,3,4,5,5,5,6,7,8,9,9,9,10]

lst_without_dups = list(set(lst))

print(random.choices(lst_without_dups, k=5))

#Output:
[9, 0, 8, 3, 6]

Hopefully this article has been useful for you to learn how to generate a list of random numbers without repeating in Python.

Other Articles You'll Also Like:

  • 1.  Check if Variable is Tuple in Python
  • 2.  Get Month Name from Date in Python
  • 3.  Using Python to Print Degree Symbol
  • 4.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 5.  Write Integer to File Using Python
  • 6.  How to Capitalize the First Letter of Every Word in Python
  • 7.  Using Python to Iterate Over Two Lists
  • 8.  Using Python to Check if Deque is Empty
  • 9.  Check if All Elements in Array are Equal in Python
  • 10.  Python math.factorial() function – Calculate Factorial of Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. Reid Karabush says

    June 15, 2022 at 3:31 am

    Interesting approach. I’m trying to produce a random list when running the same random code generator program asynchronously (simultaneously) where the output list of all combined programs are unique.

    Any ideas how to do this? I’m thinking along the lines of using your approach but after each program uses a value, remove it from the list/ set. Think that’ll work?

    Reply
    • Erik says

      June 15, 2022 at 7:42 am

      Thanks for the comment.

      Are you describing the following output?

      program_1 = [0,5,4]
      program_2 = [3,6,1]
      program_3 = [8,2,7]

      or something like this?

      program_1 = [0,5,4]
      program_2 = [3,6,5]
      program_3 = [8,2,4]

      If you are starting the different python programs from the same script as your random code generator, then you could pass the random code generator as an object and the first case can be solved.

      If you the second one is what you desire, then you can send your random code generator a different seed to random.seed() for each program to guarantee that the list is unique (but the numbers may not be unique)

      Reply
  2. Reid Karabush says

    June 15, 2022 at 8:47 am

    It’s most like the 2nd scenario. Thing is, it’s the same program being run from different Celery workers. I don’t think I can change the code for each Celery worker. Using the technique described in this article should work OK when only one worker runs the program, however what’s unique here is I’m doing Async processing using Celery and I need each instance to produce a unique list from the same code.

    Thanks,

    Reply
    • Erik says

      June 15, 2022 at 8:52 am

      Is it possible to keep a variable representing a seed which will be updated each time the Celery worker performs its work? Then each time the Celery worker is done, the seed can be updated.

      In theory, this could work. Otherwise what you could do is generate all possible permutations of your list and then just select one by one until the list is complete.

      Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy