In this video, we will write a JavaScript program to generate random alphabets.
Generating random alphabets in Javascript
Algorithm:
- 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.
- Step 2: Then we initialize the seed to the current system time so that every time a new random seed is generated.
- 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.
- Step 4: Print the random alphabet.
Here we use 2 Javascript libraries to generate random alphabets:
- 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.
- 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.