Python

Create Random Permutations of Unique Numbers from 1 to N in Python.

Write a program that produces random permutations of unique numbers from 1 to N. We will use two algorithms and compare their efficiency. Algorithm 1: Use a single list. Generate a random “R” between 1 to N. If “R” is not in the list, add it to the list, move to the next position and repeat. If “R” is already in the list, keep generating until a unique number “R” is found. Algorithm 2: Use two lists: L1 and L2. Fill L2 with the numbers from 1 to N. Generate a random index “R” between 0 and len(L2)-1. Pop the element from L2 and append the element (value) to L1. Repeat until all elements from L2 are removed.

Verified by MonsterInsights