Generate a Random Permutation of 1 to n in JavaScript Last Updated : 01 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In programming, creating a random permutation of numbers from 1 to n is a common task. A random permutation means randomly rearranging the elements that make up a permutation. Below are the methods to generate a random permutation of 1 to n in JavaScript: Table of Content Fisher-Yates (Knuth) AlgorithmUsing Built-in FunctionsFisher-Yates (Knuth) AlgorithmThe Knuth shuffle, also called the Fisher-Yates algorithm, is a popular method to generate random permutations. It replaces each element in the array with a previously chosen element at random as it iterates through the array from the last to the first element. This ensures an impartial and effective shuffle. Example: The function generates a random permutation of numbers from 1 to n using the Fisher-Yates (Knuth) Algorithm. JavaScript function generateRandomPermutation(n) { let permutation = Array .from({ length: n }, (_, i) => i + 1); for (let i = n - 1; i > 0; i--) { const j = Math .floor(Math.random() * (i + 1)); [permutation[i], permutation[j]] = [permutation[j], permutation[i]]; } return permutation; } console.log("Fisher-Yates (Knuth) Algorithm:"); console.log(generateRandomPermutation(5)); OutputFisher-Yates (Knuth) Algorithm: [ 1, 3, 2, 5, 4 ] Using Built-in FunctionsIt is also possible to create a random permutation in JavaScript by combining a random number generator with built-in functions like Array.from() and Array.prototype.sort(). Example: The function generates a random permutation of numbers from 1 to n using built-in functions. JavaScript function generateRandomPermutation(n) { let permutation = Array .from({ length: n }, (_, i) => i + 1); permutation .sort(() => Math.random() - 0.5); return permutation; } console.log("Using Built-in Functions:"); console.log(generateRandomPermutation(5)); OutputUsing Built-in Functions: [ 3, 1, 4, 2, 5 ] Comment More infoAdvertise with us Next Article Generate random alpha-numeric string in JavaScript H heysaiyad Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Generate a Random Number in JavaScript? To generate a random number in JavaScript, use the built-in methods that produce a floating-point number between 0 (inclusive) and 1 (exclusive).Below are the approaches to generate random numbers in JavaScript:Table of ContentUsing Math.random() methodUsing Math.floor() with Math.random()Using Math 2 min read Generate random alpha-numeric string in JavaScript In this article with the help of javascript we are generating the alpha-numeric string of specific length using javascript, here are some common method Approach 1: Creates a function that takes 2 arguments one is the length of the string that we want to generate and another is the characters that we 2 min read JavaScript Program to Generate Random Hex Codes of Color What is hex code? A hex code is a six-digit, three-byte hexadecimal number used to represent colors in HTML, CSS, and SVG. The bytes represent the red, green, and blue components of the color. The hex codes are an integral part of HTML for web design and are a key way of representing colors digital 1 min read How to Generate a Random Password using JavaScript? Creating a random password generator is an excellent beginner-friendly project that can be useful in real-world applications. Passwords are vital for securing accounts, and strong, randomized passwords enhance security significantly. How to Generate a Random Password using JavaScriptWhat We're Going 5 min read Print all Permutation of Array using JavaScript Permutations of Array are a fundamental concept in combinatorics and algorithms, showcasing all possible arrangements of its elements. Examples: Input: nums = [1, 2, 3] Output: [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2] ] Explanation: There are 6 possible permutations Input: 2 min read How to Generate a Random Boolean using JavaScript? To generate a random boolean in JavaScript, use Math.random(), which returns a random number between 0 and 1.Approach: Using Math.random() functionCalculate Math.random() function.If it is less than 0.5, then true otherwise false.Example 1: This example implements the above approach. JavaScript// Fu 1 min read Like