对 rand() C 的最少调用
Minimum calls to rand() C
编辑:
很抱歉因为我不详细的问题而造成的不便。
所以,我有一个包含 1000 个单元格(索引 0-999)的数字 (int) 数组
我需要通过在 C 中调用 rand() 函数来用唯一的随机数填充数组的所有单元格,但我只需要调用 1000 次函数就可以做到这一点。(生成的每个数字都插入到数组中), 数组中不能有重复的数字。
有什么办法可以做到吗?
注意:
这是一个示例代码,用于填充数组而不限制对 rand 的调用次数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DO_RAND rand()%1000
#define ARR_SIZE 1000
int main()
{
int i,callToRand=0,j;
int array[ARR_SIZE];
srand(time(NULL));
for(i=0;i<ARR_SIZE;i++) //Runs on the whole array
{
array[i]=DO_RAND; //inserting random number to the array
callToRand++; //increasing the number of calls to rand() function
for(j=0;j<i;j++) //running on the array till the current cell
{
if(array[j]==array[i])
//checking if the number has already in the array.
{
array[i]=DO_RAND;
callToRand++;
j=-1; //staring the checker loop again
}
}
}
printf("The number of calls to the function rand() were: %d\n",callToRand);
system("PAUSE");
return (0);
}
最后说你想要一个数组大小范围内的唯一数字数组,试试这个。请注意,数字不是随机的 - 它们是定义的 - 序列是。
您发布的尝试非常低效,使用了两个嵌套循环。这使用一个循环来初始化数组,另一个循环来随机化序列。无需计算对 rand()
的调用,这显然是 ARR_SIZE
.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 100 //1000
int main()
{
int i, j, temp;
int array[ARR_SIZE];
srand((unsigned)time(NULL));
for(i=0; i<ARR_SIZE; i++) // set up unique array
array[i] = i;
for(i=0; i<ARR_SIZE; i++) { // randomize the sequence
j = rand() % ARR_SIZE; // pick another (or same) index
temp = array[i]; // and swap the values
array[i] = array[j];
array[j] = temp;
}
for(i=0; i<ARR_SIZE; i++) // show results
printf ("%5d", array[i]);
printf ("\n");
return (0);
}
编辑: 很抱歉因为我不详细的问题而造成的不便。
所以,我有一个包含 1000 个单元格(索引 0-999)的数字 (int) 数组 我需要通过在 C 中调用 rand() 函数来用唯一的随机数填充数组的所有单元格,但我只需要调用 1000 次函数就可以做到这一点。(生成的每个数字都插入到数组中), 数组中不能有重复的数字。
有什么办法可以做到吗?
注意: 这是一个示例代码,用于填充数组而不限制对 rand 的调用次数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DO_RAND rand()%1000
#define ARR_SIZE 1000
int main()
{
int i,callToRand=0,j;
int array[ARR_SIZE];
srand(time(NULL));
for(i=0;i<ARR_SIZE;i++) //Runs on the whole array
{
array[i]=DO_RAND; //inserting random number to the array
callToRand++; //increasing the number of calls to rand() function
for(j=0;j<i;j++) //running on the array till the current cell
{
if(array[j]==array[i])
//checking if the number has already in the array.
{
array[i]=DO_RAND;
callToRand++;
j=-1; //staring the checker loop again
}
}
}
printf("The number of calls to the function rand() were: %d\n",callToRand);
system("PAUSE");
return (0);
}
最后说你想要一个数组大小范围内的唯一数字数组,试试这个。请注意,数字不是随机的 - 它们是定义的 - 序列是。
您发布的尝试非常低效,使用了两个嵌套循环。这使用一个循环来初始化数组,另一个循环来随机化序列。无需计算对 rand()
的调用,这显然是 ARR_SIZE
.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 100 //1000
int main()
{
int i, j, temp;
int array[ARR_SIZE];
srand((unsigned)time(NULL));
for(i=0; i<ARR_SIZE; i++) // set up unique array
array[i] = i;
for(i=0; i<ARR_SIZE; i++) { // randomize the sequence
j = rand() % ARR_SIZE; // pick another (or same) index
temp = array[i]; // and swap the values
array[i] = array[j];
array[j] = temp;
}
for(i=0; i<ARR_SIZE; i++) // show results
printf ("%5d", array[i]);
printf ("\n");
return (0);
}