The random number generator rand() is part of the standard library. Include the header file stdlib.h in your source file when you use it. The prototype for it is:
随机数生成器rand()是标准库的一部分。当您使用源文件时需要包含头文件stdlib,其原型为:
int rand(void)
Each call to rand()returns an integer randomly selected from the range 0..RAND_MAX. On many systems, RAND_MAX is 32767, but on some systems it can be much higher.
每次调用rand()都会返回一个从0-RAND_MAX范围内随机选择的整数。在许多系统上,RAND_MAX为32767,但在某些系统上,它可能会高得多。
Actually, rand() uses an algorithm and so the integers it returns are completly predictable. If you know the algorithm and the previous value returned, the next value can easily be calculated. Because they are predictable, the numbers are called pseudo-random. But for many purposes the numbers are scrambled up enough that they can be used as random numbers.
实际上,rand()使用一个算法,因此它返回的整数是完全可预测的。如果知道算法和返回的上一个值,则可以轻松计算下一个值。因为它们是可预测的,所以这些数字被称为伪随机数。但在许多情况下,这些数字被充分地置乱,可以用作随机数。
The function srand() initializes rand().
函数srand()初始化rand()。
int srand( unsigned int seed )
The seed starts the random number generator at an integer of your choice. From then on, the sequence is completely determined by the algorithm. Everytime you start the random number generator with the same seed, you get the same sequence.
种子以您选择的整数启动随机数生成器。从那时起,序列完全由算法决定。每次用相同的种子启动随机数生成器时,都会得到相同的序列。
To get a different sequence of pseudo-random numbers each time you run a program, start each run with a different seed. Without initialization, rand() produces the same stream of pseudo-random numbers every time it is used. This can be desirable since sometimes you wish to test different algorithms using the same sequence of (pseudo-)random events. For example, to compare two sorting algorithms you would want to use the same sequence of unsorted numbers for each.
要在每次运行程序时获得不同的伪随机数序列,请使用不同的种子开始每次运行。在没有初始化的情况下,rand()每次使用时都会生成相同的伪随机数流。这可能是需要的,因为有时您希望使用相同的(伪)随机事件序列测试不同的算法。例如,要比较两种排序算法,您需要对每种算法使用相同的未排序数字序列。
随机生成指定长度的字符串和一维、二维数组的 demo code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int randInt(int min,int max)
{
return rand()/(RAND_MAX+1.0)*(max-min+1)+min;
}
char* randStr(int charNums)
{
char * str = (char*)malloc(sizeof(char)*charNums+1);
for(int i=0;i<charNums;i++)
str[i] = 'a' + randInt(0,25);
str[charNums] = '\0';
return str;
}
int * randIntArr(int min,int max,int n)
{
int* arr = (int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
arr[i] = randInt(min,max);
return arr;
}
int** randInt2DArr(int min,int max,int row,int col)
{
int** arr = (int**)malloc(sizeof(int*)*row);
int i,j;
for(i=0;i<col;i++)
arr[i] = (int*)malloc(sizeof(int)*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
arr[i][j] = randInt(min,max);
return arr;
}
int main()
{
int i,j;
for(i=1;i<10;i++)
{
srand(time(NULL)*i);
printf("%s\n",randStr(10));
}
printf("\n");
int *arr= randIntArr(0,10,12);
for(i=0;i<10;i++)
printf("%d ",arr[i]);
printf("\n");
int row=5,col=6;
int **arr2d = randInt2DArr(0,100,row,col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%2d ",arr2d[i][j]);
printf("\n");
}
getchar();
}
/*
apjsqkbgim
aybjalvnja
aitaknqtjo
brmruokakc
bbeiepfhkq
bkwyorzoke
btppysuuls
cdhgitoblg
cmzxsujimu
5 2 8 2 0 6 4 5 3 1
62 60 38 84 71 100
20 54 69 23 69 25
55 93 73 75 29 73
3 37 98 69 21 41
73 92 95 38 1 38
*/
ref
http://www.programmedlessons.org/CPuzzlesNew/PartR/partRComment01.html
-End-