c语言程序中怎样用rand函数编写一个从1到33中任选6个不同的数的程序 c语言,使用随机函数rand()编写一个猜数字的游戏程序
c\u8bed\u8a00\u600e\u4e48\u4f7f\u7528\u968f\u673a\u51fd\u6570rand\u7f16\u5199\u4e00\u4e2a\u731c\u6570\u5b57\u7684\u6e38\u620f\u7a0b\u5e8f?#include
#include
#include
#define N 10
main()
{
int i,a,b;
srand(time(0)); /*\u8bbe\u7f6e\u79cd\u5b50,\u5e76\u751f\u6210\u4f2a\u968f\u673a\u5e8f\u5217*/
while(~scanf("%d",&b))
{
for(i=0;i<N;++i)rand()%10;
a = rand()%100;
while(1)
{
if(a == b){puts(" \u606d\u559c\u4f60 \u731c\u5bf9\u4e86 !\n***************\n");break;}
elseif(a > b)puts("\u4f60\u8f93\u5165\u7684\u6570 \u5c0f\u4e86!");
else puts("\u4f60\u8f93\u5165\u7684\u6570 \u5927\u4e86!");
scanf("%d",&b);
}
}
system("pause");
}
\u6d4b\u8bd5\u6570\u636e\uff1a
\u8bf7\u8f93\u5165\u60a8\u731c\u7684\u90a3\u4e2a\u6570:
10
\u7b2c1\u6b21\u731c\u6d4b\uff0c\u5f88\u9057\u61be\u60a8\u731c\u5c0f\u4e86\uff01
\u8bf7\u8f93\u5165\u60a8\u731c\u7684\u90a3\u4e2a\u6570:
15
\u7b2c2\u6b21\u731c\u6d4b\uff0c\u5f88\u9057\u61be\u60a8\u731c\u5927\u4e86\uff01
\u8bf7\u8f93\u5165\u60a8\u731c\u7684\u90a3\u4e2a\u6570:
12
\u7b2c3\u6b21\u731c\u6d4b\uff0c\u606d\u559c\u60a8\u731c\u5bf9\u4e86,\u7535\u8111\u968f\u673a\u6570\u662f12\uff01
\u4ee3\u7801\uff1a
#include "stdio.h"
#include "conio.h"
#include
#include
#include
main()
{
int randnum,count=0;
int n,isRight;
time_t t;
srand((unsigned)time(&t));
randnum=(int)(rand()%1000);
while(1)
{
printf("\n\u8bf7\u8f93\u5165\u60a8\u731c\u7684\u90a3\u4e2a\u6570:\n");
scanf("%d",&n);
count++;
if(n==randnum)
{
printf("\u7b2c%d\u6b21\u731c\u6d4b\uff0c\u606d\u559c\u60a8\u731c\u5bf9\u4e86,\u7535\u8111\u968f\u673a\u6570\u662f%d\uff01\n",count,randnum);
break;
}
else if(n>=randnum)
{
printf("\u7b2c%d\u6b21\u731c\u6d4b\uff0c\u5f88\u9057\u61be\u60a8\u731c\u5927\u4e86\uff01\n",count);
}
else
{
printf("\u7b2c%d\u6b21\u731c\u6d4b\uff0c\u5f88\u9057\u61be\u60a8\u731c\u5c0f\u4e86\uff01\n",count);
}
}
getch();
}
getch();
}
\u697c\u4e3b\u597d\u8fd0\uff01
首先,了解rand()/33 即是从0-32中任选一个数
则第一个数a = *(p+rand()/33)
然后,从剩下32数中任选一个数b,过程如下:
for(int i = a; i<33;i++){
p+a = p+a+1;
}
令b = *(p+rand()/32)
如此循环到6,自己写吧
先建一个
数组
ay=
{1,……33},
指针*p
=
ay
首先,了解rand()/33
即是从0-32中任选一个数
则第一个数a
=
*(p+rand()/33)
然后,从剩下32
数中
任选一个数b,过程如下:
for(int
i
=
a;
i<33;i++){
p+a
=
p+a+1;
}
令b
=
*(p+rand()/32)
如此循环到6,自己写吧
绛旓細婧愪唬鐮佸涓嬶細srand锛堬級*/ include main(){ int i,j;for(i=0;i<100;i++){ j=1+(int)(10.0*rand()/(RAND_MAX+1.0));printf("%d ",j);}
绛旓細include<stdio.h>#include<stdlib.h>int main(){FILE*fp;char name[15];char num[15];char ch;int n=0,m;unsigned int bb;fp=fopen("student.txt","r");while(!feof(fp)){ch=fgetc(fp);if(ch=='\n')n++;}//缁熻浜烘暟fclose(fp);bb=time(0);srand(bb);m=rand()%n+1;n=0...
绛旓細printf("%d\n", rand() % 100);return 0;} 闄や互涓婃墍璇寸殑涔嬪锛岃ˉ鍏呬竴鐐瑰氨鏄痵rand杩欎釜鍑芥暟涓瀹氳鏀惧湪寰幆澶栭潰鎴栬呮槸寰幆璋冪敤鐨勫闈紝鍚﹀垯鐨勮瘽寰楀埌鐨勬槸鐩稿悓鐨勬暟瀛椼侻SDN涓殑渚嬪瓙銆// crt_rand.c // This program seeds the random-number generator // with the time, then displays 10 random ...
绛旓細rand鏄殢鏈烘暟鍙戠敓鍑芥暟銆俽and()%26 寰楀埌涓涓0鍒25鐨勬暣鏁般(1)浣跨敤璇ュ嚱鏁伴鍏堝簲鍦ㄥ紑澶村寘鍚ご鏂囦欢stdlib.h include<stdlib.h>(C++寤鸿浣跨敤#include<cstdlib>锛屼笅鍚)(2)鍦ㄦ爣鍑嗙殑C搴涓嚱鏁皉and()鍙互鐢熸垚0~RAND_MAX涔嬮棿鐨勪竴涓殢鏈烘暟锛屽叾涓璕AND_MAX 鏄痵tdlib.h 涓畾涔夌殑涓涓暣鏁帮紝瀹冧笌绯荤粺鏈夊叧銆(3...
绛旓細include <stdio.h> include <stdlib.h> include define N 10 main(){ int i,a,b;srand(time(0)); /*璁剧疆绉嶅瓙,骞剁敓鎴愪吉闅忔満搴忓垪*/ while(~scanf("%d",&b)){ for(i=0;i<N;++i)rand()%10;a = rand()%100;while(1){ if(a == b){puts(" 鎭枩浣 鐚滃浜 !\n***\n")...
绛旓細淇敼鍚庣殑C璇█浠g爜濡備笅锛屽彧淇敼浜嗕竴澶勫湴鏂癸紝鍗冲垹闄や簡鈥渞eturn 0;鈥溿俰nclude <stdio.h>#include <stdlib.h>#include void main(){int i,j;int x[50];int n=50;FILE *fp;srand((unsigned)time(NULL));// 鐢ㄥ綋鍓嶆椂闂存潵浜х敓闅忔満鏁扮瀛愶紝杩欐牱姣忔鐢ㄦ湰绋嬪簭浜х敓鐨勯殢鏈烘暟搴忓垪 灏嗕笉鍚岋紝鏇撮殢鏈恒俧...
绛旓細10 绗1娆$寽娴嬶紝寰堥仐鎲炬偍鐚滃皬浜嗭紒璇疯緭鍏ユ偍鐚滅殑閭d釜鏁:15 绗2娆$寽娴嬶紝寰堥仐鎲炬偍鐚滃ぇ浜嗭紒璇疯緭鍏ユ偍鐚滅殑閭d釜鏁:12 绗3娆$寽娴嬶紝鎭枩鎮ㄧ寽瀵逛簡,鐢佃剳闅忔満鏁版槸12锛佷唬鐮侊細include "stdio.h"include "conio.h"include include <math.h> include<stdlib.h> main(){ int randnum,count=0;int n,isRig...
绛旓細鏈枃鐢遍潚鏉惧師鍒涘苟渚滸PL-V2鍙婂叾鍚庣画鐗堟湰鍙戞斁锛岃浆杞借娉ㄦ槑鍑哄涓斿簲鍖呭惈鏈澹版槑銆俓x0d\x0a\x0d\x0aC++涓父鐢rand()鍑芥暟鐢熸垚闅忔満鏁帮紝浣嗕弗鏍兼剰涔変笂鏉ヨ鐢熸垚鐨勫彧鏄吉闅忔満鏁帮紙pseudo-random integral number锛夈傜敓鎴愰殢鏈烘暟鏃堕渶瑕佹垜浠寚瀹氫竴涓瀛愶紝濡傛灉鍦绋嬪簭鍐呭惊鐜紝閭d箞涓嬩竴娆$敓鎴愰殢鏈烘暟鏃惰皟鐢ㄤ笂涓娆$殑...
绛旓細rand()%m杩欎釜鍑芥暟鏄殢鏈轰骇鐢0鍒癿-1鐨勯殢鏈烘暟锛涙瘮濡俽and()%10灏辨槸闅忔満浜х敓0鍒9鐨勯殢鏈烘暟銆
绛旓細鍒╃敤srand((unsigned int)(time(NULL))鏄竴绉嶆柟娉曪紝鍥犱负姣忎竴娆¤繍琛绋嬪簭鐨勬椂闂存槸涓嶅悓鐨勩傚湪C璇█閲鎵鎻愪緵鐨勯殢鏈烘暟鍙戠敓鍣ㄧ殑鐢ㄦ硶锛氱幇鍦ㄧ殑C缂栬瘧鍣ㄩ兘鎻愪緵浜嗕竴涓熀浜嶢NSI鏍囧噯鐨勪吉闅忔満鏁板彂鐢熷櫒鍑芥暟锛岀敤鏉ョ敓鎴愰殢鏈烘暟銆傚畠浠氨鏄rand()鍜宻rand()鍑芥暟銆傝繖浜屼釜鍑芥暟鐨勫伐浣滆繃绋嬪涓嬶細1) 棣栧厛缁檚rand()鎻愪緵涓涓瀛...