练习伪随机数的生成
利用比较数字游戏来实现随机数的应用
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int display(){
//显示菜单
printf("**************************************\n");
printf("1.begin\n");
printf("2.exit\n");
printf("**************************************\n");
int choice=0;
scanf("%d", &choice);
return choice;
}
void begin(){
//比较函数以及随机数的应用
int input,random;
srand((unsigned int)time(0));
random = rand()%100+1;
while (1){
printf("guess number:\n");
scanf("%d", &input);
if (input < random){
printf("低\n");
}
else if (input>random){
printf("高\n");
}
else{
printf("对\n");
Sleep(1000);//利用Sleep函数进行结果延迟
system("cls");
break;
}
}
}
void game(){
//菜单选项选择实现
while (1){
int y = display();
if (y == 1){
begin();
}
else if (y == 2){
system("cls");
break;
}
else{
system("cls");
printf("error\n");
}
}
}
int main(){
game();
system("pause");
return 0;
}
时间戳(timestamp),通常是一个字符序列,唯一地标识某一刻的时间。数字时间戳技术是数字签名技术一种变种的应用。
rand()函数定义:
http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand
srand()函数定义:
http://www.cplusplus.com/reference/cstdlib/srand/