Srand and Rand two function mates can produce a pseudo-random number sequence. The RAND function requires a system-supplied seed to generate a sequence of pseudo-random numbers before generating a random number, and Rand produces a series of random numbers based on the value of the seed. If the seed provided by the system does not change, the sequence of pseudo-random numbers generated by each call to the RAND function is the same. Srand (unsigned seed) alters the seed value provided by the system through the parameter seed, which allows the sequence of pseudo-random numbers generated by each call to the RAND function to be different, thus achieving a true "random" meaning. System time can often be used to change the system's seed value, which is srand, which can provide different seed values for the RAND function, resulting in a different sequence of random numbers.
Program 1:
1#include <stdlib.h>/*The srand function is used, so you have to have this header file*/2#include <stdio.h>3 #defineMAX 154 5 intMain ()6 {7 intNumber[max] = {0};8 inti;9Unsignedintseed;Tenprintf"manual input random number seed: \ n"); Onescanf"%d", &seed);/*manually enter the seed*/ A Srand (seed); -printf"The resulting random number is as follows: \ n"); - for(i =0; i < MAX; i++){ theNumber[i] = rand ()% -;/*generates a random integer within 100*/ -printf"%d\t", Number[i]); - } -printf"\ n"); + return 0; -}
Operation Result:
To manually enter a random number seed: 3 The resulting random number is as follows: $ - the + - + the the 1 - - - the the 2
Program 2:
1#include <stdlib.h>2#include <stdio.h>3#include <time.h>/*The time function is used, so you have to have this header file*/4 #defineMAX 155 6 intMainvoid)7 {8 intNumber[max] = {0};9 inti;TenSrand ((unsigned) time (NULL));/*Seeding son*/ Oneprintf"The resulting sequence of random numbers is as follows: \ n"); A for(i =0; i < MAX; i++) - { -Number[i] = rand ()% -;/*generates a random integer within 100*/ theprintf"%d\t", Number[i]); - } -printf"\ n"); - return 0; +}
Operation Result:
The resulting sequence of random numbers is as follows: - in the - - the Panax Notoginseng - - - Ten - - + the
assigning random values to arrays in C language