例如,一个介于 1 和 10 之间的随机数,该值更有可能为 7。
你可以做一些事情,比如从数组 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("%dn", data[rand()%11]);
return 0;
}
我从中得到的(排序)输出是:
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
请注意,7 出现的频率高于任何其他数字(大约是其两倍)。 要提高频率,请在阵列中放置更多 7。