The Wayback Machine - https://web.archive.org/web/20240530225152/https://www.geeksforgeeks.org/generate-random-alpha-numeric-string-in-javascript/
Open In App

Generate random alpha-numeric string in JavaScript

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 want to be present in the string.
  • Declare new variable ans = ‘ ‘.
  • Traverse the string in reverse order using for loop.
  • Use JavaScript Math.random() method to generate the random index and multiple with the length of the string.
  • Use JavaScript Math.floor( ) to round off it and add it into the ans.

Example 1: This example uses the Math.random() method to generate the random index and then appends the character from the string we passed. 

Javascript




function randomStr(len, arr) {
    let ans = '';
    for (let i = len; i > 0; i--) {
        ans +=
            arr[(Math.floor(Math.random() * arr.length))];
    }
    console.log(ans);
}
 
randomStr(20, '12345abcde');


Output

eecedee32d5245ba5ee3

Approach 2:

  • First, generate a random number using Math.random() method.
  • Use JavaScript toString(36) to convert it into base 36 (26 char + 0 to 9) which is also an alpha-numeric string.
  • Use JavaScript String.slice() method to get the part of the string which is started from position 2.

Example 2: This example first generates a random number(0-1) and then converts it to base 36 which is also an alpha-numeric string by using toString(36)() method. 

Javascript




function GFG_Fun() {
    console.log(
        Math.random().toString(36).slice(2));
}
GFG_Fun()


Output

vv1lcnuwtm

Similar Reads

Sort Mixed Alpha/Numeric array in JavaScript
Sorting a mixed alpha/numeric array in JavaScript involves arranging the elements in a specified order, typically lexicographically (dictionary order) for strings and numerically for numbers. When these types are mixed, a custom sort function is required to effectively handle comparisons between different types. In this article, we will explore mul
3 min read
How to Generate Random and Unique Password in Node.js using 'generate-password' NPM Module ?
The "generate-password" module is a very useful and important external NPM module that can generate unique and random passwords for our projects. One of the main advantages of using this module is that it allows developers to easily generate passwords with various requirements, such as length, complexity, and character sets. This can save developer
2 min read
How to generate a random boolean using JavaScript ?
The task is to generate random boolean value. Here we are going to use JavaScript to achieve the goal. Approach 1: Calculate Math.random() function. If it is less than 0.5, then true otherwise false. Example 1: This example implements the above approach. <!DOCTYPE HTML> <html> <head> <title> How to generate a random boolean
2 min read
Generate a Random Birthday Wishes using JavaScript
In this article, we are going to learn how to create a webpage that generates random Birthday wishes using HTML, CSS, and JavaScript. Approach: To generate a random birthday wish we will follow the below-given steps. HTML Code: In this section, we will create a basic structure of the HTML file i.e. it contains <html>, <head>, <body
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 digitally. Methods to be used for generating hex codes: Ma
1 min read
How to Generate a Random Number in JavaScript ?
JavaScript allows us to generate random numbers using Math.random() method, which return a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). To obtain random integers within a specific range, one often uses a combination of Math.random(), multiplication, and rounding. Table of Content Using Math.random() methodUsing Math.
2 min read
Generate a Random Permutation of 1 to n in JavaScript
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) Al
2 min read
How to generate random number in given range using JavaScript ?
A random number is a value that comes out of a process without any pattern or predictability. In JavaScript, you can create random numbers using a function called Math.random(). This function generates a decimal number between 0 (inclusive) and 1 (exclusive). By multiplying this number and manipulating it, you can get the range and precision of ran
2 min read
Generate Random Characters & Numbers in JavaScript
Generate random characters and numbers in JavaScript utilizing Math.random for random numbers and String.fromCharCode() for generating random strings. Combining Math.random and ch.charAt() generates a random mix of characters and numbers within a specified range. Below are the approaches to Generate random characters and numbers in JavaScript: Tabl
2 min read
Generate N Random Numbers Between a Range & Pick the Greatest in JavaScript
Given n random numbers between a range, our task is to pick the greatest number and print it using JavaScript. Example: Input: arr[] = {4, 6, 8, -5, -4, 4} Output: 8Explanation: Maximum number is 8Table of Content Using a loopUsing Array methods Using a loopIn this approach, we use a loop to iteratively generate random numbers, and also while itera
2 min read