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)
Sentry
sentry.io โบ sentry answers โบ javascript โบ generate random string/characters in javascript
Generate random string/characters in JavaScript | Sentry
This function takes in the length of the random string that you want as an argument and then creates a string output where each character is randomly chosen from the chars variable. In this case, the possible characters in the random string are alphanumeric. Random characters are chosen using Math.random().
Videos
06:09
How to Generate a RANDOM STRING in JavaScript and HTML - YouTube
03:29
How to Generate Random String in Javascript - YouTube
01:43
How to Generate a Random String in JavaScript (Secure & Simple ...
12:47
Javascript Project for Beginners | Code a random strings generator ...
10:53
The Fastest Way To Generate Random Strings in JavaScript - YouTube
00:16
Generate a random string in the terminal CMD #nextjs #code ...
npm
npmjs.com โบ package โบ randomstring
randomstring - npm
January 10, 2025 - $ npm install -g randomstring $ randomstring > sKCx49VgtHZ59bJOTLcU0Gr06ogUnDJi $ randomstring 7 > CpMg433 $ randomstring length=24 charset=github readable > hthbtgiguihgbuttuutubugg
ยป npm install randomstring
Published ย Jan 10, 2025
Version ย 1.3.1
Author ย Elias Klughammer
Repository ย https://github.com/klughammer/node-randomstring
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Math โบ random
Math.random() - JavaScript | MDN
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range โ which you can then scale to your desired range. The implementation selects the initial seed to the random ...
Programiz
programiz.com โบ javascript โบ examples โบ generate-random-strings
JavaScript Program to Generate Random String
// program to generate random strings // declare all characters const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; function generateString(length) { let result = ' '; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(generateString(5));
Medium
medium.com โบ @modos.m98 โบ creating-a-seeded-random-string-generator-in-javascript-3165aae1c2d5
Creating a Seeded Random String Generator in JavaScript | by Mohammad Hossein Mazandaranian | Medium
August 10, 2025 - By using a seed value, you can generate the same sequence every time you run your code with that seed. In this article, weโll build a simple seeded random string generator in JavaScript. Weโll start with a seeded pseudo-random number generator (PRNG), then use it to create customizable random strings.
Reddit
reddit.com โบ r/learnjavascript โบ random string
r/learnjavascript on Reddit: Random String
December 21, 2021 -
I am new to programming because of my school but how can I make the program choose a random String of the available Strings i have given to him. How can i lead this whole process from beginning to the end I am actually confused because I tried something with array and java.util.Random, but it seems to only do the work with integers in "x"
Thank youu
Top answer 1 of 2
2
Add the strings to an array const words = ['jon','jack','jill'] Math.random() generates a random number between 0-1 in Javascript. Math.floor() rounds a number down to an interger. let randomIndex = Math.floor(Math.random()*words.length + 1) Then access the string from the array, based on the random index. console.log(words[randomIndex])
2 of 2
1
stackoverflow
Squash
squash.io โบ how-to-generate-random-string-characters-in-javascript
How To Generate Random String Characters In Javascript
In this example, the generateRandomString() function takes a parameter length which specifies the desired length of the random string. The function initializes an empty string result and a string of allowed characters characters.
WsCube Tech
wscubetech.com โบ resources โบ javascript โบ programs โบ generate-random-strings
JavaScript Program to Generate a Random String (6 Programs)
October 31, 2025 - Learn 6 easy JavaScript programs to generate random strings. Explore different methods using Math.random(), charAt() and more with simple examples.
W3Schools
w3schools.com โบ js โบ js_random.asp
W3Schools.com
JS Examples JS HTML DOM JS HTML ... JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Math.random() always returns a number lower than 1....
Scaler
scaler.com โบ home โบ topics โบ random string generator in javascript
Random String Generator in JavaScript - Scaler Topics
January 1, 2024 - This code defines a function generateRandomString that takes a length parameter for the desired string length and a charset parameter for the character set to choose from. It then iterates length times, selecting random characters from the provided charset and concatenating them to create a random string generator javascript.