c ++产生不同的随机数_C ++程序生成随机密码
c ++產生不同的隨機數
Problem Statement:
問題陳述:
Write a menu driven program to generate password randomly
編寫菜單驅動程序以隨機??生成密碼
constraint:
約束:
password should consist of
密碼應包含
- lowercase Alphabet - a to z
- UpperCase Alphabet - A to Z
- Number - 0 to 9
- Special Symbol - !,@,#,$,%,&
Password length should be
密碼長度應為
- Minimum - 7
- Maximum - 100
Password should begin with a letter (can be lowercase or uppercase)
密碼應以字母開頭(可以為小寫或大寫)
Password should contain at least 2 lowercase letter , 2 uppercase letter, 1 number , and 1 special symbol
密碼至少應包含2個小寫字母,2個大寫字母,1個數字和1個特殊符號
Don't make use of any library function like rand() or srand().
不要使用諸如rand()或srand()之類的任何庫函數。
Each time generated password should be unique.
每次生成的密碼應該是唯一的。
用C ++生成隨機密碼的程序 (Program to generate random password in C++)
Note: Program is compiled and executed on Code Block IDE (version 17.12) using GNU GCC Compiler on windows platform
注意: 在Windows平臺上使用GNU GCC編譯器在代碼塊IDE(版本17.12)上編譯和執行程序
#include <bits/stdc++.h> using namespace std;//selectArray is a utility function that is used to //randomly generate a integer in the range 1 to 4 (both inclusive) int selectArray() {srand(time(NULL));int i = rand() % 5;if (i == 0)i++;return i; }//getKey() is another utility function that is used to randomly generate //an integer in the range 0 to 25 (both inclusive) int getKey() {srand(time(NULL));int key = rand() % 26;return key; } void generate_password(int length) {//Intializing result string password as NULL.string password = "";//Strings whose characters will be used to build passwordstring alphabet = "abcdefghijklmnopqrstuvwxyz";string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";string s_symbol = "[email?protected]#$%&";string number = "0123456789";//initializing local variablesint key, count_alphabet = 0, count_ALPHABET = 0, count_number = 0, count_s_symbol = 0;//Count will store the length of the password being created,//initially this will be zero(0)int count = 0;while (count < length) {// selectArray() function will return a number 1 to 4// and will use to select one of the above defined string//(i.e alphabet or ALPHABET or s_symbol or number )// 1 is for string alphabet// 2 is for string ALPHABET// 3 is for string number// and 4 is for string s_symbolint k = selectArray();//for the first character of password it is mentioned that,//it should be a letter//so the string that should be selected is either alphabet or //ALPHABET (i.e 1 or 2)//following if condition will take care of it.if (count == 0) {k = k % 3;if (k == 0)k++;}switch (k) {case 1:// following if condition will check if minimum requirement of alphabet// character has been fulfilled or not// in case it has been fulfilled and minimum requirements of other// characters is still left then it will break ;if ((count_alphabet == 2) && (count_number == 0 || count_ALPHABET == 0 || count_ALPHABET == 1 || count_s_symbol == 0))break;key = getKey();password = password + alphabet[key];count_alphabet++;count++;break;case 2:// following if condition will check if minimum requirement of// ALPHABET character has been fulfilled or not// in case it has been fulfilled and minimum requirements of// other characters is still left then it will break ;if ((count_ALPHABET == 2) && (count_number == 0 || count_alphabet == 0 || count_alphabet == 1 || count_s_symbol == 0))break;key = getKey();password = password + ALPHABET[key];count_ALPHABET++;count++;break;case 3:// following if condition will check if minimum requirement// of Numbers has been fulfilled or not// in case it has been fulfilled and minimum requirements of// other characters is still left then it will break ;if ((count_number == 1) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 1 || count_ALPHABET == 0 || count_s_symbol == 0))break;key = getKey();key = key % 10;password = password + number[key];count_number++;count++;break;case 4:// following if condition will check if minimum requirement of// Special symbol character has been fulfilled or not// in case it has been fulfilled and minimum requirements of// other characters is still left then it will break ;if ((count_s_symbol == 1) && (count_alphabet == 0 || count_alphabet == 1 || count_ALPHABET == 0 || count_ALPHABET == 1 || count_number == 0))break;key = getKey();key = key % 6;password = password + s_symbol[key];count_s_symbol++;count++;break;}}cout << "\n-----------------------------\n";cout << " Password \n";cout << "------------------------------\n\n";cout << " " << password;cout << "\n\nPress any key continue \n";getchar(); } int main() {int opt, length;//Menudo {cout << "\n-----------------------------\n";cout << " Random Password Generator\n";cout << "------------------------------\n\n";cout << " 1. Generate Password"<< "\n";cout << " 2. Exit"<< "\n\n";cout << "Press key 1 to Generate Password and key 2 to exit : ";cin >> opt;switch (opt) {case 1:cout << "Enter Length : ";cin >> length;//if length is less than 7 , program will show errorif (length < 7) {cout << "\nError : Password Length Should be atleast 7\n";cout << "Press any key to try again \n";getchar();}// Length should not exceed 100 , program should show error if it exceedselse if (length > 100) {cout << "\nError : Maximum length of password should be 100\n";cout << "Press any key to try again \n";getchar();}//Otherwise call generate_password() function to generate passwordelsegenerate_password(length);break;default:// If invalid option is chosen by user it will also show errorif (opt != 2) {printf("\nInvalid choice\n");printf("Please Press ( 1 ) to generate password and ( 2 ) to exit.\n");cout << "Press any key to try again \n";getchar();}break;}} while (opt != 2);return 0; } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Output
輸出量
-----------------------------Random Password Generator ------------------------------1. Generate Password2. ExitPress key 1 to Generate Password and key 2 to exit : 1 Enter Length : 16-----------------------------Password ------------------------------w#yS2S!youMue6ywPress any key continue-----------------------------Random Password Generator ------------------------------1. Generate Password2. ExitPress key 1 to Generate Password and key 2 to exit : 1 Enter Length : 50-----------------------------Password ------------------------------cE8o!UAO#k6Wi8cCK2!c4kMuq!W2eY40!eaS!oEwi2E#0u!yi#Press any key continue-----------------------------Random Password Generator ------------------------------1. Generate Password2. ExitPress key 1 to Generate Password and key 2 to exit : 2Process returned 0 (0x0) execution time : 24.358 s Press any key to continue.翻譯自: https://www.includehelp.com/cpp-programs/generate-random-password.aspx
c ++產生不同的隨機數
總結
以上是生活随笔為你收集整理的c ++产生不同的随机数_C ++程序生成随机密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工智能ai 学习_人工智能中学习代理的
- 下一篇: innodb是如何存数据的?yyds