// crt_rand.c // This program seeds the random-number generator // with the time, then exercises the rand function. //#include <stdlib.h> #include <stdio.h> #include <time.h>void SimpleRandDemo( int n ) { // Print n random numbers. int i; for( i = 0; i < n; i++ ) printf( " %6d\n", rand() ); }void RangedRandDemo( int range_min, int range_max, int n ) { // Generate random numbers in the half-closed interval // [range_min, range_max). In other words, // range_min <= random number < range_max int i; for ( i = 0; i < n; i++ ) { int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min; printf( " %6d\n", u); } }int main( void ) { // Seed the random-number generator with the current time so that // the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); SimpleRandDemo( 10 ); printf("\n"); RangedRandDemo( -100, 100, 10 ); }
#include "GAPublic.h"
#include"ENIGame.h"
#include "GACGame.h"
class ENIGame;
class GACGame;
//模板类
//传入迭代值,返回其位置
//template<class LISTTYPE>
//int GetLocationFromIter(std::list<LISTTYPE> listob,std::list<LISTTYPE>::iterator iter);
ENIGame* ENIGameOb_static=NULL;
//引擎与GAME连接
int ENIGame::GameSystemFuntion(HWND hwnd)
{
ENIGameOb_static=new GACGame(hwnd);
ENIGameOb_static->ENd3dOb=NULL;
ENIGameOb_static->ENIGame::hWnd=hwnd;// cout<<"GameSystemFuntion()"<<endl;
return 0;
}//公共//生成随机数
int PUGetRandom(int min,int max)
{
// cout<<"new-->temp:"<<endl;
int temp=0;
double count=0;
while(1)
{
count+=rand()%500000;
//生成随机种子
/// srand(GetTickCount()*rand()%(500)+rand()%(1000));
srand(GetTickCount()+count);
temp=rand()%(max+1);
// cout<<"-->temp:"<<temp<<endl;
if(temp>=min && temp<=max)
{
return temp;
}
}
// cout<<"end-->temp:"<<endl;
}
//检测所有数据,是否有未被使用过
bool PUCheckAllNotUseed(int array[],int size)
{
int i=0;
for(i=0;i<=size;i++)
{
if(array[i]==0)//有没有被使用过
{
return true;
}
}
return false;//全被使用过
}//检测单个数据,是否等此值
bool PUCheckData(int array[],int location,int value)
{
if(array[location]==value)//相等
{
return true;
}
return false;//不相等
}
//设置一个值,按时间控制其大小
float PUCalculate(float &data,float speed,float time,float &savetime,float timespeed,float max,float min)
{
if(speed==0)
{
return data;
}
if(savetime+timespeed<=time)
{
savetime=time;
data+=speed;
}
if(data>max)
{
data=max;
}
else if(data<min)
{
data=min;
}
return data;
}
//求2点间的夹角
float GetPointSAngle(D3DXVECTOR3 &p1,D3DXVECTOR3 &offset1,D3DXVECTOR3 &p2,D3DXVECTOR3 &offset2)
{
float angle=0;
//2点的向量
D3DXVECTOR3 line1=(p1+D3DXVECTOR3(1,0,0))-p1,line2=p2-p1;
D3DXVec3Normalize(&line1,&line1);
D3DXVec3Normalize(&line2,&line2);
angle=ENIGameOb_static->ENd3dOb->ENmathOb->Dot(line1, line2);
return angle;
}
2)保证每组rand之间的间隔,在每组之前调用一次srand
3)采用伪随机数算法。
for(int i=0;i <10;i++)
{
rand()
...
..
.
}
把里面的0去掉
srand(time());
这样每次就不一样了
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//#include <stdlib.h>
#include <stdio.h>
#include <time.h>void SimpleRandDemo( int n )
{
// Print n random numbers.
int i;
for( i = 0; i < n; i++ )
printf( " %6d\n", rand() );
}void RangedRandDemo( int range_min, int range_max, int n )
{
// Generate random numbers in the half-closed interval
// [range_min, range_max). In other words,
// range_min <= random number < range_max
int i;
for ( i = 0; i < n; i++ )
{
int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
+ range_min;
printf( " %6d\n", u);
}
}int main( void )
{
// Seed the random-number generator with the current time so that
// the numbers will be different every time we run.
srand( (unsigned)time( NULL ) ); SimpleRandDemo( 10 );
printf("\n");
RangedRandDemo( -100, 100, 10 );
}