Random Shuffle PythonThe random package of Python has a built-in function shuffle(). A sequence can be shuffled using it (like a list or a tuple) in Python; shuffling means changing the indices of the elements of a collection. Syntax of random.shuffle()We use the shuffle() function to change the indices of the elements of any iterable randomly. This function does not return a new list. It alters the original list. Syntax of the shuffle() function Parameters of the function: sequence: This can take any Python iterable object. function: This parameter is optional. The default value of this function is the random() function. If we pass a user-defined function, then the function must return a value in the range (0, 1]. Returns: This function does not return anything. Example of the shuffle() FunctionShuffling a List Code Output: The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list the first time: [4, 3, 1, 6, 5, 2] After shuffling the list a second time: [5, 6, 4, 2, 1, 3] Shuffling The List Using a User-Defined FunctionCode Output: The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list the first time: [1, 6, 2, 5, 3, 4] After shuffling the list a second time: [1, 6, 2, 5, 3, 4] Randomly Shuffle Not in PlaceAs we know, the Python shuffle() function operates on the given collection in place and does not return anything or returns a None, which means that it randomly rearranges the elements of the original list and modifies it in its memory location. However, we generally require our original list or collection. The method described now allows us to retain our original list and get a shuffled list. This alternative returns a new, scrambled list rather than altering the original one. Before altering the order, we will make a copy of our original list to preserve it. Code Output: The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list: [5, 2, 6, 4, 3, 1] Shuffling Two Lists Simultaneously in the Same OrderLet us assume we have two Python lists. We need to shuffle these two lists to maintain the order of shuffling. For instance, one list contains alphabets, and the other contains corresponding numbers. Let's see how we can shuffle two lists and maintain their order. Code Output: The first list: ['a', 'b', 'c', 'd', 'e'] The second list: [1, 2, 3, 4, 5] The first list after shuffling: ('c', 'e', 'a', 'd', 'b') The second list after shuffling: (3, 5, 1, 4, 2) Shuffling NumPy Multidimensional ArrayThe Numpy module of Python has a package named numpy.random. The package is used to generate random data. In this example, we will use the Numpy module of Python to create a 2D array. Then we will use the numpy.random.shuffle() function to shuffle this multidimensional array. Code Output: The original 2D array [[ 1 3] [ 5 7] [ 9 11] [13 15] [17 19] [21 23]] The shuffled 2D array [[13 15] [ 1 3] [21 23] [17 19] [ 9 11] [ 5 7]] Shuffling an Item and Getting the Same Result Every TimeIn this section, we will see how to use the seed method of the random package to shuffle a list so that every time we call the shuffle function, it will shuffle the original list in the same order. Thus every time, we will get the same list after shuffling. How Does a PRNG Work? Python's random library is not completely random. It uses a method called Pseudo-Random Number Generator (PRNG). This means that it is a deterministic system. The seed value of the random module acts as the basis of random number generation. By default, the random package takes the current system time as its seed value. We can change this seed value, and thus we can change the output of the random.shuffle() function. Let us see how to use this value. Code Output: The original list is: [1, 3, 5, 7, 9, 11, 13, 15] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] Next Topic_new_ in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India