Can you randomize a list in python?

Putting a list into random order might seem like an unusual task, but it can be quite useful for many businesses. For example, you might want to randomly assign leads to salespeople, assign jobs to employees or conduct a raffle for customers. Whatever the reason, you can use the popular programming language Python to randomize the order of a list of elements.

If you're generating random lists or random numbers for a sensitive purpose, such as a prize draw with money at stake or cryptographic purposes, you'll want to make sure you're using a high-quality random number generator.

Shuffle a List in Python

You can use Python to build a list randomizer tool in just a few lines of code. Python is free for both commercial and personal use and available for all modern operating systems, including Microsoft Windows, macOS, Linux and other Unix-style systems.

Python 2 and Python 3, the two versions of the programming language in widespread use, include a function called shuffle that can randomize a list or another sequence of data. To use shuffle, import the Python random package by adding the line import random near the top of your program.

Then, if you have a list called x, you can call random.shuffle(x) to have the random shuffle function reorder the list in a randomized way.

Note that the shuffle function replaces the existing list. If you want to keep a copy of the list in its original order, make a copy of the list before shuffling it. You can import the Python copy package and use its copy method to do so. Use y = copy.copy(x) to create a copy of list x and assign variable y to refer to it. Note that if you write y = x, variable y is assigned to point to the same list as x does, and a new copy is not created.

Grab a Random Element

If you only want to grab a random element from a list in Python, you can do this with the random package as well. Import the random package by including import random near the top of your code.

To choose a single element, use random.choice(x), where x is the name of your list. The function returns a single, randomly selected element from list x. Note that if you call random.choice multiple times, you may get the same element of the list multiple times unless you delete it from the list in between calls.

If you want to grab a set of multiple elements that doesn't include the same element twice, use random.sample (x, k), where x is the list and k is the number of elements you want. If a list includes repeated elements, they might be repeated in the randomized sample.

Risks With Random Number Generators

Not all random number generators are created equal. The Python documentation warns that Python's built-in random number generator isn't suitable for cryptographic purposes, where a minimum level of actual randomness is required to create random number generators to encrypt data with little risk of third-party decryption. If it's important to your business that the list is truly random and unpredictable, such as for a raffle drawing, it's important to use the right random number generator.

Some subclasses of the Python random module provide increased levels of randomness. On modern operating systems, you can ask the operating system to provide random data of a quality usable for cryptography. Through Python, you can access this random data using the function os.urandom in the os module, or you can call random.SystemRandom to generate a random number generator equivalent to the random module using the operating system random data.

To use this with shuffle, type r = random.SystemRandom() to generate the random number generator and then call r.shuffle(x) on your list x. Other functions, including choice and sample, can also be used with the SystemRandom generator.

Check your operating system's documentation to understand how its random number generator works and if it's suitable for your needs. In some cases, you may be able to configure the operating system to use an external hardware device to generate random numbers.

I receive as input a list of strings and need to return a list with these same strings but in randomized order. I must allow for duplicates - same string may appear once or more in the input and must appear the same number of times in the output.

I see several "brute force" ways of doing that (using loops, god forbid), one of which I'm currently using. However, knowing Python there's probably a cool one-liner do get the job done, right?

asked Jun 20, 2009 at 18:05

Can you randomize a list in python?

Roee AdlerRoee Adler

32.8k32 gold badges102 silver badges132 bronze badges

1

>>> import random
>>> x = [1, 2, 3, 4, 3, 4]
>>> random.shuffle(x)
>>> x
[4, 4, 3, 1, 2, 3]
>>> random.shuffle(x)
>>> x
[3, 4, 2, 1, 3, 4]

answered Jun 20, 2009 at 18:09

John KugelmanJohn Kugelman

337k66 gold badges509 silver badges559 bronze badges

2

Given a string item, here is a one-liner:

''.join([str(w) for w in random.sample(item, len(item))])

marr75

5,6521 gold badge25 silver badges41 bronze badges

answered Aug 25, 2010 at 17:45

Yann VRYann VR

4765 silver badges8 bronze badges

0

You'll have to read the strings into an array and then use a shuffling algorithm. I recommend Fisher-Yates shuffle

answered Jun 20, 2009 at 18:12

Charles MaCharles Ma

45.3k22 gold badges85 silver badges100 bronze badges

1

In python 3.8 you can use the walrus to help cram it into a couple of lines First you have to create a list from the string and store it into a variable. Then you can use random to shuffle it. Then just join the list back into a string.

random.shuffle(x := list("abcdefghijklmnopqrstuvwxyz"))
x = "".join(x)

answered Apr 16, 2020 at 17:39

Can you randomize a list in python?

MikeologistMikeologist

3683 silver badges11 bronze badges

import random

b = []
a = int(input(print("How many items you want to shuffle? ")))
for i in range(0, a):
    n = input('Please enter a item: ')
    b.append(n)

random.shuffle(b)

print(b)

hotfix

3,34618 silver badges32 bronze badges

answered Sep 26, 2018 at 8:01

Can you randomize a list in python?

0

How do I create a randomizer in Python?

import random n = random. random() print(n).
import random n = random. randint(0,22) print(n).
import random randomlist = [] for i in range(0,5): n = random. randint(1,30) randomlist. ... .
import random #Generate 5 random numbers between 10 and 30 randomlist = random. sample(range(10, 30), 5) print(randomlist).

How do you randomize a list?

Assuming you have a list of names in column A, please follow these steps to randomize your list:.
Insert a new column next to the list of names you want to randomize. ... .
In the first cell of the inserted column, enter the RAND formula: =RAND().
Copy the formula down the column..

How do you randomize a list index in Python?

Use the random. sample() function when you want to choose multiple random items from a list without repetition or duplicates. There is a difference between choice() and choices() . The choices() was added in Python 3.6 to choose n elements from the list randomly, but this function can repeat items.