想要num个均匀分布随机数,然后在输出不是我想要的值,怎么回事儿啊,如果将genrand中的malloc去掉,在main函数中加malloc,就对了,这是为什么啊?
void genrand(int num,double *randarray)
{
/*======================================================================================================
++++++++++++++++++++++++++++++++++++产生num个[0,1]之间的均匀分布随机数++++++++++++++++++++++++++++++++++++++++++++
========================================================================================================*/
//
// 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
// 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
// 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
int i;
randarray=(double*)malloc((num*sizeof(double)));
srand( (unsigned int)time(0) );
for (i = 0; i < num; i++)
{
randarray[i] = rand()/(double)(RAND_MAX);//获得0-1的浮点数
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <math.h>
#include <stdarg.h>
#include "stdio.h"
#include "stdlib.h"
#include "RRT.h"
int main()
{
int i;
int num=4;
double *randarray=0;
double c=4;
randarray=&c;
//randarray=(double*)malloc((num*sizeof(double)));
genrand(num, randarray);
for(i=0;i<num;i++)
{
printf("%lf\n",randarray[i]);
}
return 0;
}