BOOL makeRands(const int nCount, const DWORD* pInArray, DWORD* pOutArray)
{
if ( 0>=nCount || NULL==pInArray || NULL==pOutArray )
{
return FALSE;
}
// pOutArray[i] 表示 pInArray[i] 的位序
srand( (unsigned)time( NULL ) );
for (int i=0; i<nCount; i++)
{
int RANGE_MIN = 0;
int RANGE_MAX = nCount;
if (i==0) pOutArray[i] = rand()%(RANGE_MAX-RANGE_MIN) + RANGE_MIN;
else
{
while(1)
{
int flag=0;
int value = rand() % ( RANGE_MAX - RANGE_MIN ) + RANGE_MIN;
for(int j=0; j<i; j++)
{
if( pOutArray[j] == value )
{
flag = 1;
break;
}
}
if ( flag != 1 )
{
pOutArray[i] = value;
break;
}
}
}
}
// pOutArray[i] 表示 pInArray[i] 的位序
for (int i=0; i<nCount; i++)
{
int m = pOutArray[i];
pOutArray[i] = pInArray[m];
}
return TRUE;
}
{
if ( 0>=nCount || NULL==pInArray || NULL==pOutArray )
{
return FALSE;
}
// pOutArray[i] 表示 pInArray[i] 的位序
srand( (unsigned)time( NULL ) );
for (int i=0; i<nCount; i++)
{
int RANGE_MIN = 0;
int RANGE_MAX = nCount;
if (i==0) pOutArray[i] = rand()%(RANGE_MAX-RANGE_MIN) + RANGE_MIN;
else
{
while(1)
{
int flag=0;
int value = rand() % ( RANGE_MAX - RANGE_MIN ) + RANGE_MIN;
for(int j=0; j<i; j++)
{
if( pOutArray[j] == value )
{
flag = 1;
break;
}
}
if ( flag != 1 )
{
pOutArray[i] = value;
break;
}
}
}
}
// pOutArray[i] 表示 pInArray[i] 的位序
for (int i=0; i<nCount; i++)
{
int m = pOutArray[i];
pOutArray[i] = pInArray[m];
}
return TRUE;
}
本文介绍了一个用于生成指定数量随机数并对其进行排序的C语言函数。该函数首先使用时间戳作为种子来初始化随机数生成器,然后生成一系列不重复的随机整数,并将这些整数按其在输入数组中的原始位置进行排序。
966

被折叠的 条评论
为什么被折叠?



