[英]How to generate array of random numbers using functions in C
我正在嘗試將程序從Matlab移植到C,以提高執行時間和內存使用率。 由於C中沒有函數會生成0和1的均勻填充數組(或者我找不到它們),因此我創建了一個函數來根據相關的概率填充數組。
例如 :如果該概率是0.3(30%)予想有100個元素的陣列,具有零和一隨機填充,並且陣列中的一些的總和必須接近30。
我使用N =試驗次數,而不是100,因此,如果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);
}
請給我一些解決問題的技巧。 我已經嘗試過使用不同類型的值( long代替int , double代替float ),但是沒有任何結果。 先感謝您!
干杯,
Pullo86
您幾乎肯定想做的就是傳遞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);
/* … */
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.