week 8

来源:互联网 发布:淘宝在线美工 编辑:程序博客网 时间:2024/04/29 08:31

用rand编写一个程序,出来一个随机数

/* Note:Your choice is C IDE */#include "stdio.h"#include "stdlib.h"main(){    int ia;    ia=rand();    printf("the random number is %d \n",ia);}
随机输出1~45之间的数
/* Note:Your choice is C IDE */#include "stdio.h"#include "stdlib.h"main(){    int ia,ib;    ia=rand();    ib=ia%45+1;    printf("the random number is %d \n",ib);}
随机输出10个数

/* Note:Your choice is C IDE */#include "stdio.h"#include "stdlib.h"main(){    int ia,ib,i;    for(i=0;i<10;i++)    {    ia=rand();        ib=ia%45+1;        printf("the random number is %d \n",ib);    }
}

随机输出10个没有重复数字的数

/* Note:Your choice is C IDE */#include "stdio.h"#include "stdlib.h"#define N 25main(){    int ia,ib,i,j;    int students[N];    for(i=0;i<N;i++)    {    ia=rand();        ib=ia%45+1;        if(i>0)/*因为第一个数不可能重复 */        {        for(j=0;j<=i-1;j++)        {        if(ib==students[j])        {        ib=rand()%45+1;        j=-1;/*j=-1,继续返回执行for循环*/        }        }        }        students[i]=ib;        printf("the random number is %d \n",ib);    }}


0 0