- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate random characters and numbers in JavaScript?
At first, set the characters and numbers −
var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
To generate randomly, use Math.random(). Following is the code −
Example
function generateRandom3Characters(size) { var generatedOutput= ''; var storedCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var totalCharacterSize = storedCharacters.length; for ( var index = 0; index < size; index++ ) { generatedOutput+=storedCharacters.charAt(Math.floor(Math.random() * totalCharacterSize)); } return generatedOutput; } console.log(generateRandom3Characters(3));
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo136.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo136.js lq4
- Related Articles
- Generate random string/characters in JavaScript?
- How to generate random numbers between two numbers in JavaScript?
- Generate random numbers in Arduino
- Generate n random numbers between a range and pick the greatest in JavaScript
- Generate pseudo-random numbers in Python
- Generate Random Integer Numbers in Java
- Generate random numbers using C++11 random library
- How to generate random whole numbers in JavaScript in a specific range?
- Generate Random Long type numbers in Java
- Java program to generate random numbers
- How does Python generate random numbers?
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters
- Excel random data: generate random numbers, texts, dates, times in Excel
- Generate 10 random four-digit numbers in Java
- How to generate large random numbers in Java?

Advertisements