Program to generate random alphabets

23 Mar, 2022
Prerequisite : rand() and srand() Given all alphabets in a character array, print a string of random characters of given size.We will use rand() function to print random characters. It returns random integer values. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called.  
 
A ubiquitous use of unpredictable random characters is in cryptography, which underlies most of the schemes which attempt to provide security in modern communications (e.g. confidentiality, authentication, electronic commerce, etc).Random numbers are also used in situations where “fairness” is approximated by randomization, such as selecting jurors and military draft lotteries.Random numbers have uses in physics such as electronic noise studies, engineering, and operations research. Many methods of statistical analysis, such as the bootstrap method, require random numbers.
A ubiquitous use of unpredictable random characters is in cryptography, which underlies most of the schemes which attempt to provide security in modern communications (e.g. confidentiality, authentication, electronic commerce, etc).
Random numbers are also used in situations where “fairness” is approximated by randomization, such as selecting jurors and military draft lotteries.
Random numbers have uses in physics such as electronic noise studies, engineering, and operations research. Many methods of statistical analysis, such as the bootstrap method, require random numbers.
Pseudo code : 1. First we initialize two character arrays, one containing all the alphabets and other of given size n to store result.2. Then we initialize the seed to current system time so that every time a new random seed is generated.3. Next, we use for loop till n and store random generated alphabets. Below is C++ implementation of above approach :  
C++
Java
C#
Javascript
// CPP Program to generate random alphabets#include <bits/stdc++.h>using namespace std; const int MAX = 26; // Returns a string of random alphabets of// length n.string printRandomString(int n){    char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',                          'h', 'i', 'j', 'k', 'l', 'm', 'n',                          'o', 'p', 'q', 'r', 's', 't', 'u',                          'v', 'w', 'x', 'y', 'z' };     string res = "";    for (int i = 0; i < n; i++)        res = res + alphabet[rand() % MAX];         return res;} // Driver codeint main(){   srand(time(NULL));   int n = 10;   cout << printRandomString(n);   return 0;}
// JAVA Program to generate random alphabetsimport java.util.*; class GFG{ static int MAX = 26; // Returns a String of random alphabets of// length n.static String printRandomString(int n){    char []alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',                        'h', 'i', 'j', 'k', 'l', 'm', 'n',                        'o', 'p', 'q', 'r', 's', 't', 'u',                        'v', 'w', 'x', 'y', 'z' };     String res = "";    for (int i = 0; i < n; i++)        res = res + alphabet[(int) (Math.random() * 100 % MAX)];         return res;} // Driver codepublic static void main(String[] args){    int n = 10;    System.out.print(printRandomString(n));}} // This code is contributed by Rajput-Ji
// C# Program to generate random alphabetsusing System; class GFG{static int MAX = 26; // Returns a String of random alphabets of// length n.static String printRandomString(int n){    char []alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',                        'h', 'i', 'j', 'k', 'l', 'm', 'n',                        'o', 'p', 'q', 'r', 's', 't', 'u',                        'v', 'w', 'x', 'y', 'z' };     Random random = new Random();    String res = "";    for (int i = 0; i < n; i++)        res = res + alphabet[(int)(random.Next(0, MAX))];         return res;} // Driver codepublic static void Main(){    int n = 10;    Console.Write(printRandomString(n));}}
<script>// JAVAscript Program to generate random alphabetslet MAX = 26; // Returns a String of random alphabets of// length n.function printRandomString(n){    let alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g',        'h', 'i', 'j', 'k', 'l', 'm', 'n',        'o', 'p', 'q', 'r', 's', 't', 'u',        'v', 'w', 'x', 'y', 'z'];     let res = "";    for (let i = 0; i < n; i++)    {        res = res + alphabet[Math.floor(Math.random() * 10) % MAX];    }     return res;} // Driver codelet n = 10;document.write(printRandomString(n)); // This code is contributed by gfgking.</script>
urdfwootzr
This program will print different characters every time we run the code. 
Rajput-Ji
gfgking
jackk787878
Arrays
C++ Programs
Technical Scripter
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ program for hashing with chaining