#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
float rnd(){
//(float) RAND_MAX 代表将变量RAND_MAX 强制转换为float
return rand()/(float) RAND_MAX;
}
int main(){
srand(2);// 随机种子确定, 随机情况就确定了
printf("float = %f" , rnd());
return 0;
}
// Returns a random value between 0.0 and 1.0
猜数字游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mylibs.h"
int rnd1();//函数的原型声明
int main() {
// time(NULL) 是时间戳 返回的是1970年1月1日到现在的毫秒数
// unsigned int 无符号整型
srand((unsigned int)time(NULL));
int generate_number = rnd1();
// printf("pgenerate_number =%d \n", generate_number);
int guess_number;
while (1){
printf("please input a number\n");
scanf("%d", &guess_number);
// printf("guess_number = %d\n", guess_number);
if (guess_number > generate_number){
printf("too large\n");
} else if(guess_number < generate_number){
printf("too small\n");
}else{
printf("right ! \n");
break;
}
}
return 0;
}