Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle a list within a specific range python

I'm very new to Python, so bear with me. I would like to run a program that will shuffle a string of integers in a specific range, but without having to input each integer within that range. I.e., I want to randomize the list of integers from 1 to 100 without typing out (1, 2, 3...100).

Yes, I've looked at other answers to similar questions, but all are asking for one integer within a specific range, or are not answered for Python, or are asking for something way more complex. Thanks

like image 952
common_currency Avatar asked Jan 20 '13 19:01

common_currency


People also ask

How do you shuffle a nested list in Python?

The random. shuffle() function makes it easy to shuffle a list's items in Python. Because the function works in-place, we do not need to reassign the list to itself, but it allows us to easily randomize list elements.

How do you randomize a list in a list Python?

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.

How do you randomly shuffle a list with seeds in Python?

Python Random seed() Method The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator.


2 Answers

You can use range() along with list() to generate a list of integers, and then apply random.shuffle() on that list.

>>> lst = list(range(1,11))
>>> lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> random.shuffle(lst)
>>> lst
[4, 5, 3, 9, 2, 10, 7, 1, 6, 8]

Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object
 |
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 |  These are exactly the valid indices for a list of 4 elements.
 |  When step is given, it specifies the increment (or decrement).
like image 100
Ashwini Chaudhary Avatar answered Oct 06 '22 16:10

Ashwini Chaudhary


In case you want the entire population within a given range, As @Ashwini proposed you can use random.shuffle

In Case you are interested in a subset of the population, you can look forward to use random.sample

>>> random.sample(range(1,10),5)
[3, 5, 2, 6, 7]

You may also use this to simulate random.shuffle

>>> random.sample(range(1,10),(10 - 1))
[4, 5, 9, 3, 2, 8, 6, 1, 7]

Note, The advantage of using random.sample over random.shuffle, is , it can work on iterators, so in

  • Python 3.X you don;t need to convert range() to list
  • In Python 2,X, you can use xrange
  • Same Code can work in Python 2.X and 3.X
like image 10
Abhijit Avatar answered Oct 06 '22 16:10

Abhijit