I think this will work for you:
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
console.log(makeid(5));
Answer from csharptest.net on Stack Overflow Top answer 1 of 16
3768
I think this will work for you:
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
console.log(makeid(5));
2 of 16
3232
//Can change 7 to 2 for longer results.
let r = (Math.random() + 1).toString(36).substring(7);
console.log("random", r);
Note: The above algorithm has the following weaknesses:
- It will generate anywhere between 0 and 6 characters due to the fact that trailing zeros get removed when stringifying floating points.
- It depends deeply on the algorithm used to stringify floating point numbers, which is horrifically complex. (See the paper "How to Print Floating-Point Numbers Accurately".)
Math.random()may produce predictable ("random-looking" but not really random) output depending on the implementation. The resulting string is not suitable when you need to guarantee uniqueness or unpredictability.- Even if it produced 6 uniformly random, unpredictable characters, you can expect to see a duplicate after generating only about 50,000 strings, due to the birthday paradox. (sqrt(36^6) = 46656)
Top answer 1 of 16
3768
I think this will work for you:
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
console.log(makeid(5));
2 of 16
3232
//Can change 7 to 2 for longer results.
let r = (Math.random() + 1).toString(36).substring(7);
console.log("random", r);
Note: The above algorithm has the following weaknesses:
- It will generate anywhere between 0 and 6 characters due to the fact that trailing zeros get removed when stringifying floating points.
- It depends deeply on the algorithm used to stringify floating point numbers, which is horrifically complex. (See the paper "How to Print Floating-Point Numbers Accurately".)
Math.random()may produce predictable ("random-looking" but not really random) output depending on the implementation. The resulting string is not suitable when you need to guarantee uniqueness or unpredictability.- Even if it produced 6 uniformly random, unpredictable characters, you can expect to see a duplicate after generating only about 50,000 strings, due to the birthday paradox. (sqrt(36^6) = 46656)
Videos
03:29
How to Generate Random String in Javascript - YouTube
01:43
How to Generate a Random String in JavaScript (Secure & Simple ...
06:09
How to Generate a RANDOM STRING in JavaScript and HTML - YouTube
Generate Random Strings with Variable Lengths in JavaScript
12:47
Javascript Project for Beginners | Code a random strings generator ...
10:53
The Fastest Way To Generate Random Strings in JavaScript - YouTube
Programiz
programiz.com › javascript › examples › generate-random-strings
JavaScript Program to Generate Random String
In the above example, the Math.random() ... a random character is generated. // program to generate random strings const result = Math.random().toString(36).substring(2,7); console.log(result);...
Medium
medium.com › @randomstr › generating-random-strings-in-javascript-and-its-frameworks-118cb1c9cba7
Generating Random Strings in JavaScript and Its Frameworks | by Random STR | Medium
December 1, 2023 - function generateRandomString(length) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(generateRandomString(10)); // Outputs a 10-character random string · React developers can integrate the same function within components: import React from 'react'; function MyComponent() { const randomString = generateRandomString(10); return <div>{randomString}</div>; } export default MyComponent; Node.js offers a more secure way to generate random strings using the crypto module:
Sling Academy
slingacademy.com › article › ways-to-generate-random-strings-in-javascript
4 Ways to Generate Random Strings in JavaScript - Sling Academy
February 25, 2023 - It helps us randomly select characters ... numbers) by using the ... const generateRandomString = (length) => { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for (let i = 0; i < length; ...
GitHub
gist.github.com › 6174 › 6062387
Generate a random string in JavaScript In a short and fast way! · GitHub
const idString = async (string_length) => { var random_str = ""; var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZqeytrpolkadjsghfgmnbzxcvnQPOWEYRKASJHDGFMNBCVX--___-_jsfhrlg-_124903564576986483658fgh4sdfh687e4h897WETHJ68F7G4688471877GFHJFFGJ87469857468746hfghwrtiyj4598yhdjkhgnk"; for (let index = 0; index < string_length; index++) { random_str += characters.charAt( Math.floor(Math.random() * characters.length) ); } let string = `${random_str}`; console.log(string); return string; };
Scaler
scaler.com › home › topics › random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
January 1, 2024 - ... In this example, we use the generateRandomString function to create a random string generator javascript of length 6 by selecting characters from the ASCII range '0' to '9'. It generates random character codes within this range and converts ...
Javatpoint
javatpoint.com › random-string-generator-using-javascript
Random String Generator using JavaScript - javatpoint
Random String Generator using JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
TecAdmin
tecadmin.net › generate-random-string-in-javascript
Generate a Random String in JavaScript– TecAdmin
April 26, 2025 - In JavaScript, strings are represented by a sequence of characters between two double quotes (""). Since strings are just a sequence of characters, we can use the Math.random() method to generate a random string. Let’s look at a basic example where we construct a random string using the Math.random() method.
TutorialsPoint
tutorialspoint.com › generate-random-string-characters-in-javascript
Generate random string/characters in JavaScript?
On running the above script, the output window will pop up, displaying the random string generated of length 2 on the webpage. This is because the event gets triggered as soon as the user runs the script, and it will produce a different random string as long as the user runs the script. Let's look into the another example, where we are using the Math.random()
Media College
mediacollege.com › internet › javascript › number › random.html
How to Create Random Numbers & Characters with JavaScript
<script language="javascript" type="text/javascript"> function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var string_length = 8; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); ...
Medium
medium.com › @python-javascript-php-html-css › how-to-generate-a-random-5-character-string-in-javascript-f35accc66cbd
How to Use JavaScript to Create a Random String of Five Characters
August 24, 2024 - In this article, we will explore the most effective way to generate a 5-character string using characters from the set [a-zA-Z0–9]. By the end of this guide, you’ll have a clear understanding of how to implement this functionality in your JavaScript projects. ... In the first script, we use JavaScript to generate a random 5-character string.
npm
npmjs.com › package › random-string-generator
random-string-generator - npm
March 3, 2025 - random('uppernumeric'); // '8DOUIL7RW8MW' or others · To generate upper case numeric characters. example
» npm install random-string-generator