我正在尝试将程序从Matlab移植到C,以便改善执行时间和内存使用。 由于C中没有生成0和1的统一填充数组的函数(或者我找不到它们),因此我创建了一个函数来相应地填充相关概率的数组。
示例:如果概率为0.3(30%)我希望有一个包含100个元素的数组,随机填充零和一些,并且数组之和必须接近30。
而不是100我使用N =试验次数,所以如果N = 10000则数组之和应该接近3000,等等。
Matlab中的函数只是:
function [y] = rand_gen(N,Prob)
for i=1:N
y(i,1) = binornd(1,Prob);
end
end
该函数将被称为:
array = rand_gen(N, Probability);
现在,这是C中的问题:当我运行程序时,我可以生成一些数组,但最大数量的试验(N)非常低,我不认为它是一个记忆问题。如果我运行N = 100000的程序一切顺利,但在100000(和其他东西)崩溃后。 C中的程序是:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int * rand_gen(int N, double Prob); // Function declaration
int sum_array(int a[], int num_elements); // Function declaration
int main() // Main function
{
int N; // Declaration: Number of trials
printf("Number of trials: ");
scanf("%d", &N); // Asks for Number of trials
double Prob_a = 0.5; // Probability (50%)
int i; // Declaration: Index
int sum; // Declaration: sum of elements of arrays
int bin_array_0[N]; // Declaration: array populated randomly by 0 and 1
int bin_array_1[N]; // Declaration: array populated randomly by 0 and 1
int bin_array_2[N]; // Declaration: array populated randomly by 0 and 1
int bin_array_3[N]; // Declaration: array populated randomly by 0 and 1
int *ptrnd = NULL; // Declaration: pointer to array
int seed = time(NULL); // Declaration: random number generator seed (based on current time)
srand(seed);
ptrnd = rand_gen(N, Prob_a); // Populate a temporary array with 0 and 1 using the rand_gen function
for(i=0 ; i<N ; i++)
{
bin_array_0[i] = *(ptrnd + i);
}
/* Print the sum of ones in the array
* in order to check the rand_gen probability
* reliability and compare to the temp array */
sum = sum_array(bin_array_0, N);
printf("\n The sum of the bin_array_0 is %d\n", sum);
ptrnd = rand_gen(N, Prob_a); // Populate a temporary array with 0 and 1 using the rand_gen function
for(i=0 ; i<N ; i++)
{
bin_array_1[i] = *(ptrnd + i);
}
/* Print the sum of ones in the array
* in order to check the rand_gen probability
* reliability and compare to the temp array */
sum = sum_array(bin_array_1, N);
printf("\n The sum of the bin_array_1 is %d\n", sum);
ptrnd = rand_gen(N, Prob_a); // Populate a temporary array with 0 and 1 using the rand_gen function
for(i=0 ; i<N ; i++)
{
bin_array_2[i] = *(ptrnd + i);
}
/* Print the sum of ones in the array
* in order to check the rand_gen probability
* reliability and compare to the temp array */
sum = sum_array(bin_array_2, N);
printf("\n The sum of the bin_array_2 is %d\n", sum);
ptrnd = rand_gen(N, Prob_a); // Populate a temporary array with 0 and 1 using the rand_gen function
for(i=0 ; i<N ; i++)
{
bin_array_3[i] = *(ptrnd + i);
}
/* Print the sum of ones in the array
* in order to check the rand_gen probability
* reliability and compare to the temp array */
sum = sum_array(bin_array_3, N);
printf("\n The sum of the bin_array_3 is %d\n", sum);
return(0);
}
// Function: generate an array populated by 0 and 1 according to a uniformed distribution
int * rand_gen(int N, double Prob)
{
int sum; // Declaration: sum of elements of arrays
int *array;
array = (int *)malloc(sizeof(int)*N);
if(array == NULL)
{
printf("\nRun out of memory!\n");
exit(1);
}
int j;
double x;
for(j=0; j<N; j++)
{
x=rand();
x=x/RAND_MAX;
if (x < Prob)
{
array[j]=1;
}
else
{
array[j]=0;
}
}
/* Print the sum of ones in the array
* in order to check the rand_gen probability
* reliability and compare to the bin_array_* */
sum = sum_array(array, N);
printf("\n The sum of the temp array is %d\n", sum);
return array;
}
// Function: sum elements of array and return the sum.
int sum_array(int a[], int num_elements)
{
int k, sum=0;
for (k=0; k<num_elements; k++)
{
sum = sum + a[k];
}
return(sum);
}
请给我一些如何解决问题的提示。我已尝试使用不同类型的值(长而不是 int 和 double 而不是 float )但没有任何结果。 提前谢谢!
干杯,
Pullo86
答案 0 :(得分:0)
你几乎肯定想要做的是将rand_gen()
一个指针传递给它要填充的数组,然后让它写入,而不是让它分配大型数组然后复制和泄漏。 (这是一个应用程序,其中包含<random>
和<vector>
的C ++ STL可能更适合。)
但是,如果你想使用这个基本结构,请声明:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/* Set N. */
bool *binarray[] = {
rand_gen( N, prob_a ), // binarray[0]
rand_gen( N, prob_a ),
rand_gen( N, prob_a ),
rand_gen( N, prob_a ) // binarray[3]
};
static const size_t m = sizeof(binarray)/sizeof(binarray[0]);
/* Do stuff with binarray[0] through binarray[m-1]. */
for ( size_t i = 0; i < m; ++i ) {
free(binarray[i]);
binarray[i] = NULL;
}
但实际上,您可能想要一个随机生成器更新的数组数组,例如:
bool *fill_arrays( size_t m, size_t n, bool to_fill[m][n], double prob )
{
for ( size_t i = 0; i < m; ++i )
rand_gen_in_place( to_fill[i], n, prob );
return to_fill;
}
void do_stuff_with( size_t m, size_t n, bool binarray[m][n] );
int main(void)
{
/* … */
bool* arrayp = calloc( M*N, sizeof(bool) );
fill_arrays( M, N, arrayp, prob_a );
do_stuff_with( M, N, arrayp );
free(arrayp);
/* … */
}