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.
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 :
This program will print different characters every time we run the code.
- C++ program to generate random number
- Generate a random permutation of 1 to N
- Generate random String of given size in Java
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Remove consecutive alphabets which are in same case
- Count of alphabets having ASCII value less than and greater than k
- Make alphabets using the elements of an array
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Longest sub-array with equal number of alphabets and numeric characters
- Puzzle | Random Seating
- random header | Set 2 (Distributions)
- random header in C++ | Set 1(Generators)
- Find two numbers whose divisors are given in a random order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.