生成的随机数在指定的范围之间,最大值为nMax,最小值味nMin
int GenIntData(int nNum,int* pData,int nMin,int nMax)
{
if (nNum <= 0 || pData == NULL || nMin < 0 || nMax < 0 || nMax < nMin)
{
return -1;
}
srand(time(NULL));
int i;
for ( i = 0; i < nNum; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (nMax - nMin);
/*printf( "%6x\n", u);*/
*pData++ = u;
}
return 0;
}
测试程序
int GenIntData(int nNum,int* pData,int nMin,int nMax);
int _tmain(int argc, _TCHAR* argv[])
{
int* pIntData = new int[1024];
GenIntData(1024,pIntData,0,0xFFFF);
int* p = pIntData;
for (int i = 0; i < 1024;i++)
{
printf("%d\n",*p++);
}
delete[] pIntData;
return 0;
}
阅读(1560) | 评论(0) | 转发(0) |