Random Shuffle Algorithm for Word List

  • Share this:

Code introduction


This function takes a list of words as input and shuffles their order using the random shuffle algorithm.


Technology Stack : random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def shuffle_words(words):
    """
    洗牌算法打乱一个单词列表。
    """
    shuffled_words = words[:]
    for i in range(len(shuffled_words) - 1, 0, -1):
        j = random.randint(0, i)
        shuffled_words[i], shuffled_words[j] = shuffled_words[j], shuffled_words[i]
    return shuffled_words                
              
Tags: