你可以做一些事情,比如从数组1-10中拉出一个随机值,其中7出现两次
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
// note that 7 appears twice
int data[11] = { 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10 };
int i;
srand((unsigned)time(NULL));
for(i = 0; i < 100; i++)
printf("%d\n", data[rand()%11]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从中得到的(排序)输出是:
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
5
5
5
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
9
10
10
10
10
10
10
10
10
10
10
10
10
10
Run Code Online (Sandbox Code Playgroud)
请注意,7出现频率高于任何其他数字(大约两倍频繁).要增加频率,请在阵列中放入更多7个.