JavaScript Program to Generate Random String
Math.random() with a string of characters and pick random characters in a loop like this: function randomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; }.Examples
How to Think About It
Algorithm
Code
function randomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * chars.length); result += chars.charAt(randomIndex); } return result; } console.log(randomString(8));
Dry Run
Let's trace generating a random string of length 3 through the code
Initialize variables
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', result = ''
First iteration (i=0)
randomIndex = 12 (example), result = '' + chars.charAt(12) = 'M'
Second iteration (i=1)
randomIndex = 35 (example), result = 'M' + chars.charAt(35) = 'Mz'
Third iteration (i=2)
randomIndex = 2 (example), result = 'Mz' + chars.charAt(2) = 'MzC'
Return result
Return 'MzC'
| Iteration | randomIndex | Character Added | Result So Far |
|---|---|---|---|
| 1 | 12 | M | M |
| 2 | 35 | z | Mz |
| 3 | 2 | C | MzC |
Why This Works
Step 1: Character set
We create a string chars with all letters and digits to choose from randomly.
Step 2: Random selection
For each character needed, we use Math.random() to pick a random index in chars.
Step 3: Building the string
We add each randomly chosen character to result until it reaches the desired length.
Alternative Approaches
function randomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; return Array.from({ length }, () => chars.charAt(Math.floor(Math.random() * chars.length))).join(''); } console.log(randomString(8));
function randomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const array = new Uint32Array(length); window.crypto.getRandomValues(array); return Array.from(array, num => chars[num % chars.length]).join(''); } console.log(randomString(8));
Complexity: O(n) time, O(n) space
Time Complexity
The code runs a loop for the length of the string, so time grows linearly with the requested length.
Space Complexity
The result string grows with the length, so space used is proportional to the output size.
Which Approach is Fastest?
The simple loop and the Array.from methods have similar speed; using crypto is slower but more secure.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop with Math.random | O(n) | O(n) | General use, easy to understand |
| Array.from with map | O(n) | O(n) | Concise code, functional style |
| crypto.getRandomValues | O(n) | O(n) | Security-sensitive applications |
Math.floor(Math.random() * chars.length) to get a random index safely within the character set.Math.floor, which can cause errors by producing decimal indexes.