/*---------------------------------------
Exercise by random name
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"};/* exercise no */
While (I <count)
{
K ++;
Srand (time (0) + K * k );
Randnumber = rand () %432;
If (randnumber <= 131 & randnumber> = 101)
{
Printf ("students with student ID % d do exercises % s \ n", randnumber, ex_id [I]);
I ++; continue;
}
If (randnumber <= 231 & randnumber> = 201)
{
Printf ("students with student ID % d do exercises % s \ n", randnumber, ex_id [I]);
I ++; continue;
}
If (randnumber <= 331 & randnumber> = 301)
{
Printf ("students with student ID % d do exercises % s \ n", randnumber, ex_id [I]);
I ++; continue;
}
If (randnumber <= 431 & randnumber> = 401)
{
Printf ("students with student ID % d do exercises % s \ n", randnumber, ex_id [I]);
I ++; continue;
}
}
Getch ();
}
/*--------------------------------------*/
The function rand () in the Standard C library can generate 0 ~ A random number between rand_max. rand_max is an integer defined in stdlib. H, which is related to the system.
The rand () function is referenced directly by the expression rand () without any input parameter. For example, you can use the following statement to print two random numbers:
Printf ("random numbers are: % I \ n", Rand (), Rand ());
Because the rand () function generates integers in the specified order, the two identical values are printed each time the preceding statement is executed, therefore, the C language is not random in the positive sense.
ForProgramEach execution generates a random value of a new sequence. We usually provide a new random seed for the random number generator. The srand () function (from stdlib. h) can spread seeds for the random number generator. As long as the seeds are different, the Rand () function will generate different random number sequences. Srand () is called the initialization tool of the random number generator.
Routine:
File Name:Rand_srand.c
/* This program generates and prints ten random integers between 1 and rand_max */
# Include <stdio. h>
# Includ <stdlib. h>
Int main ()
{
Usigned int seed;/* declare the initialization seed. Note that the usigned int type */
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;
}
You will find that when the seeds you provide are the same, the random number sequence is also the same. In addition, when the seed is 1, it is the same as when the srand () function is not used, that is, the initial seed value of the Rand () function is 1 by default;
In stdlib. H, the two functions are prototype:
Int rand ();
Void srand (unsigned INT );
Expansion:
X = rand () % 11;/* generates 1 ~ Random integer between 10 */
Y = rand () % 51-25;/* generate-25 ~ Random integer between 25 */
Z = (double) rand ()/rand_max) * (B-A) + A;/* random number on the generation interval [a, B */