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 password
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string s_symbol = "[email protected]#$%&";
string number = "0123456789";
//initializing local variables
int 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_symbol
int 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;
//Menu
do {
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 error
if (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 exceeds
else 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 password
else
generate_password(length);
break;
default:
// If invalid option is chosen by user it will also show error
if (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;
}
Output
输出量
-----------------------------
Random Password Generator
------------------------------
1. Generate Password
2. Exit
Press key 1 to Generate Password and key 2 to exit : 1
Enter Length : 16
-----------------------------
Password
------------------------------
w#yS2S!youMue6yw
Press any key continue
-----------------------------
Random Password Generator
------------------------------
1. Generate Password
2. Exit
Press 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 Password
2. Exit
Press key 1 to Generate Password and key 2 to exit : 2
Process 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 ++程序生成随机密码相关推荐
- mysql语句随机数_程序生成随机数与SQL语句生成随机数
随机数可以通过程序生成,也可以通过SQL语句生成.通过程序生成随机数时一般采用硬件的编号+时间作为种子,这种方法在瞬间插入数据库N条数据的时候会影响随机数的效果,生成很多相邻的插入值相同.所以频繁插入 ...
- c语言产生随机数_C语言 求的近似值
点击上方"蓝字"关注我们 愉快的一天,不得不做的三件事: 种田,锄地,整代码!!! [题目] 用循环方法实现. (1)请利用"正多边形逼近"的方法求出π的近似值 ...
- c语言连续生成不同随机数_C语言随机数生成教程,C语言rand和srand用法详解
在实际编程中,我们经常需要生成随机数,例如,贪吃蛇游戏中在随机的位置出现食物,扑克牌游戏中随机发牌. 在C语言中,我们一般使用 头文件中的 rand() 函数来生成随机数,它的用法为: int ran ...
- 揭开ASP.NET生成随机密码的面纱
不知道从何时开始流行这种注册验证方式 .在开发需要用户注册后才能使用提供的各项功能的应用程序时,在新用户提交注册信息后,较常见的做法是由程序生成随机密码,然后发送密码到用户注册时填写的电子信箱,用户再 ...
- Collections.sort()排序使用TimSort排序报Comparison method violates its general contract 原因
前段时间升级JDK后,之前的功能报java.lang.IllegalArgumentException: Comparison method violates its general contract ...
- JNI_编程技术__网文整理
Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 Chap 3:javah命令帮助信息... 16 Chap 4:用javah产生一个.h文件... 17 Chap5:j ...
- ruby 生成随机字符串_Ruby程序生成随机数
ruby 生成随机字符串 产生随机数 (Generating random number) The task is to generate and print random number. 任务是生成 ...
- C程序生成一定范围内的随机数
Random numbers just numbers that lie within a range and any of the numbers can occur. 随机数只是在一个范围内的数字 ...
- c语言求阶乘和的流程图_C/C++编程笔记:C语言 rand() 随机函数,深入解析程序随机数!...
各种编程语言返回的随机数(确切地说是伪随机数)实际上都是根据递推公式计算的一组数值,当序列足够长,这组数值近似满足均匀分布. C的标准函数库提供一随机数生成器rand(定义在stdlib.h),能返回 ...
最新文章
- Udacity机器人软件工程师课程笔记(三)-样本搜索和找回-基于漫游者号模拟器-使用moviepy输出测试视频
- 充分利用UC berkeleys数据科学专业
- php 获取delete蚕丝_php结合Redis实现100万用户投票项目,并实时查看到投票情况的案例...
- Webpack实战(二):基础配置入门 - webpack-dev-server的介绍与用法
- 用python实现一个按需生成用于vim跳转的tags文件的小程序
- dart map 转list_Dart 集合类型List Set Map循环forEach map where any every
- 一个API方式存取日志文件的模块[VB]
- Android权限全记录(转)
- 20145326蔡馨熠《信息安全系统设计》第7周学习总结
- VirtualBox中,Windows虚拟机与主机共享文件夹不能用之一例
- 百度富文本编辑器的使用
- oracle 查看表历史记录,Oracle 查看表操作历史记录并恢复
- 打发时间的网站,收藏起来吃鸡玩腻了玩玩这些,够你玩一年
- 【最终省二】全国大学生数学建模大赛-参赛经历
- 惠州生物实验室建设宝典
- 曼卡尔M1投影仪怎么样?和哈趣H1对比哪款更好用?
- 深入理解Toll-Free Bridging
- IDEA Java 死锁
- 华为OD机试真题 Python 实现【快递投放问题】【2023 Q1 | 100分】
- 【深度学习】CNN 中 1x1 卷积核的作用