C语言程序设计 随机函数的使用-随机点名做习题

/* ---------------------------------------
随机点名 做习题
Author: emanlee
Date: 2008-04-24
--------------------------------------- */

#include "stdio.h"
#include "conio.h"
#include "time.h"

void main()
{
int count=4;
int i=0,k=0;
int randnumber;
int seed;
char ex_id[4][10]={"1.1","1.2","1.3","1.4"} ;  /* 习题编号*/

while(i<count)
{
k++;
srand(time(0)+k*k);
randnumber =rand()%432;
if(randnumber<=131 && randnumber>=101  )
{
printf("学号为 %d 的同学做习题 %s\n\n",randnumber,ex_id[i]);
i++; continue;
}
if(randnumber<=231 && randnumber>=201  )
{
printf("学号为 %d 的同学做习题 %s\n\n",randnumber,ex_id[i]);
i++;continue;
}
if(randnumber<=331 && randnumber>=301  )
{
printf("学号为 %d 的同学做习题 %s\n\n",randnumber,ex_id[i]);
i++;continue;
}
if(randnumber<=431 && randnumber>=401  )
{
printf("学号为 %d 的同学做习题 %s\n\n",randnumber,ex_id[i]);
i++; continue;
}
}
getch();
}

/* --------------------------------------*/

rand（）函数没有输入参数，直接通过表达式rand（）来引用；例如可以用下面的语句来打印两个随机数：
printf("Random numbers are: %i %i\n",rand(),rand());

/* This program generates and prints ten random integers between 1 and RAND_MAX*/
#include <stdio.h>
#includ <stdlib.h>
int main()
{
usigned int seed;        /*申明初始化器的种子，注意时usigned int 型的*/
int k;
pringt("Enter a positive integer seed value: \n");
scanf("%u",&seed);
srand(seed);
printf("Random Numbers are:\n");
for(k = 1; k <= 10; k++)
printf("%i",rand());
printf("\n");
return 0;
}

int rand();
void srand (unsigned int);

x = rand()%11; /*产生1~10之间的随机整数*/
y = rand()%51 - 25; /*产生-25 ~ 25之间的随机整数*/
z = ((double)rand()/RAND_MAX)*(b-a) + a;/*产生区间[a,b]上的随机数*/

=======================

#include <stdio.h>
#include <stdlib.h>
#include <time.h> //用到了time函数
int main()
{ int i,number;
srand((unsigned) time(NULL)); //用时间做种，每次产生随机数不一样
for (i=0; i<50; i++)
{
number = rand() % 101; //产生0-100的随机数
printf("%d ", number);

return 0;
}

=======================

posted @ 2008-04-24 23:18  emanlee  阅读(...)  评论(... 编辑 收藏