• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
November 08, 2022 |1.5K Views

Javascript program to generate random alphabets

  Share  1 Like
Description
Discussion

In this video, we will write a JavaScript program to generate random alphabets.

Generating random alphabets in Javascript

Algorithm:

  1. Step 1: First we create a function, and initialize two character arrays, one containing all the alphabets and the other of the given size n to store the result.
  2. Step 2: Then we initialize the seed to the current system time so that every time a new random seed is generated.
  3. Step 3: Next, we use for loop in which we generate a random number to use as an index for the array and then concatenate that randomly chosen alphabet to the empty string.
  4. Step 4: Print the random alphabet.

Here we use 2 Javascript libraries to generate random alphabets:

  1. Math.random(): The Math.random() function is used to return a floating-point pseudo-random number between ranges. This random number can be scaled according to the desired range.
  2. Math. floor(): The Math.floor() method is used to round off the number passed as a parameter to its nearest integer in a Downward direction of rounding i.e. towards the lesser value.