本文整理汇总了C++中rand函数的典型用法代码示例。如果您正苦于以下问题:C++ rand函数的具体用法?C++ rand怎么用?C++ rand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rand函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rand
void Snake_t::gen_target(){
target.set_coordinates(rand()%d_width, rand()%d_height);
}
示例2: random
inline float random(int howbig) {
return (float) (((float) (fmod(rand(),RAND_MAX))/RAND_MAX))*howbig;
}
示例3: getRandom
float Map::getRandom()
{
random = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);// Random value between 0.0f -> 1.0f
return random;
}
示例4: main
int main(int argc, char **argv)
{
double timeA, timeB;
Sphere *spheres;
Vec3 p,c,e;
int n;
timeA = omp_get_wtime();
n = argc>1 ? atoi(argv[1]) : N; if(n<N)n=100;
spheres = malloc(n*sizeof(*spheres));
// position, radius, surface color, reflectivity, transparency, emission color
Vec3_new(&p, 0, -10004, -20);
Vec3_new1(&c, 0.2);
Sphere_new0(&spheres[0], &p, 10000, &c, 0, 0.0);
Vec3_new(&p, 0, 0, -20);
Vec3_new(&c, 1.00, 0.32, 0.36);
Sphere_new0(&spheres[1], &p, 4, &c, 1, 0.5);
Vec3_new(&p, 5, -1, -15);
Vec3_new(&c, 0.90, 0.76, 0.46);
Sphere_new0(&spheres[2], &p, 2, &c, 1, 0.0);
Vec3_new(&p, 5, 0, -25);
Vec3_new(&c, 0.65, 0.77, 0.97);
Sphere_new0(&spheres[3], &p, 3, &c, 1, 0.0);
Vec3_new(&p, -5.5, 0, -15);
Vec3_new(&c, 0.90, 0.90, 0.90);
Sphere_new0(&spheres[4], &p, 3, &c, 1, 0.0);
// light
Vec3_new(&p, 0, 20, -30);
Vec3_new0(&c);
Vec3_new1(&e, 3);
Sphere_new(&spheres[5], &p, 3, &c, 0, 0, &e);
if(n>N){ int i,j,k; Real r,d; Vec3 v;
srand48(13);
for(i=N;i<n;i++){
k=0;
do{
Vec3_new(&p, 12*drand48()-6,9*drand48()-4,-35*drand48()-15);
r = 0.1 + drand48();
for(j=0;j<i && r>=0.1;j++){
Vec3_subs(&v,&spheres[j].center,&p);
d=Vec3_length(&v)-spheres[j].radius;
if(d<r)r=d;
}
}while(r<0.1 && ++k<1000);
if(r>=0.1){
Vec3_new(&c, rand()%4/3.0,rand()%4/3.0,rand()%4/3.0);
Sphere_new0(&spheres[i], &p, r, &c,
drand48()<0.8?0:1, drand48()<0.8?0:0.8);
}else printf("#");
}
}
if(argc>2){spheres[1].radius=0.4; spheres[1].radius2=0.16;}
printf("Calculando...\n"); fflush(stdout);
render(n, spheres);
timeB = omp_get_wtime();
double timeC = timeB - timeA;
printf("Se ha calculado en %f segundos\n",timeC);
free(spheres);
return 0;
}
示例5: Reset
void Reset()
{
MortalWound_Timer = 10000 + rand()%5000;
SpawnHatchlings_Timer = 6000 + rand()%6000;
SpawnSpawns_Timer = 15000 + rand()%30000;
}
示例6: rand_my
double rand_my()
{
double v = ((double) rand() / (RAND_MAX));
return v;
}
示例7: main
int
main(int argc, char *argv[], char *envp[])
{
FILE *fdata; /* .data file (input)*/
FILE *fmodel; /* .model file (output) */
FILE *fauc; /* .model.1st file (output) */
int atnl; /* at newline (flag) */
char *aucname; /* file name of the AUC file */
double baseline;
char c;
int divider; /* #of bins to divide in this round */
int failures_seen; /* in this bin */
int i, j, k, l;
int maxrounds; /* max(@ROUNDS) */
int numbins; /* misnomer, really "this round" */
char *sca0, *sca, *scc; /* tmp variables */
int t; /* XXX - some kind of counter? */
int this_first, this_last;
double this_pauc;
double randnum;
int *rand_seed = NULL;
int total_failures;
int num_scores;
int init_permute_flag = 1;
int unknown_meth = REL_ORDER;
Score *p;
Score *best;
double min_auc;
gargc = argc;
gargv = argv;
genvp = envp;
/*
* Make sure we grok NaNs
* (unknown entries in the input file, given as '?', are
* stored as not-a-number values)
*/
if (!isnan(nan("")))
errx(1, "Implementation does not understand NaNs");
/*
* PARSE ARGUMENTS
*/
if (argc < 3)
usage();
if ((fdata = fopen(argv[1], "r")) == NULL)
err(1, "cannot open %s for reading", argv[1]);
if ((fmodel = fopen(argv[2], "w")) == NULL)
err(1, "cannot open %s for writing", argv[2]);
if ((aucname = (char *)calloc(strlen(argv[2]) + sizeof (".1st"), 1)) == NULL)
err(1, "allocating aucname");
strcpy(aucname, argv[2]);
strcat(aucname, ".1st");
if ((fauc = fopen(aucname, "w")) == NULL)
err(1, "cannot open %s for writing", aucname);
argc -= 3;
argv += 3;
while (argc > 0) {
if (!strcmp(argv[0], "rounds") || !strcmp(argv[0], "--rounds")) {
if (argc < 2)
usage();
if ((sca0 = sca = strdup(argv[1])) == NULL)
err(1, "strdup: copying %s", argv[1]);
while (*sca == ',') /* strip leading commas, if any */
sca++;
scc = sca + strlen(sca);
if (scc == sca) /* must have at least one digit! */
usage();
while (*--scc == ',') /* strip trailing commas */
*scc = '\0';
if (strchr(sca, ',')) {
/*
* comma-separated list of rounds, parse
*/
n_rounds = 0;
for (scc = sca; *scc; scc++)
if (*scc == ',')
n_rounds++;
n_rounds++;
if ((rounds = (int *)calloc(n_rounds, sizeof (*rounds))) == NULL)
err(1, "calloc %d rounds", n_rounds);
for (i = 0; i < n_rounds; i++) {
rounds[i] = strtol(sca, &scc, 10);
if (rounds[i] <= 0)
errx(1, "round %d must be positive", i);
//.........这里部分代码省略.........
示例8: UpdateAI
void UpdateAI(const uint32 diff)
{
if (!m_creature->SelectHostilTarget() || !m_creature->getVictim())
return;
//Check for Frost Bolt
if (FrostBolt_Timer < diff)
{
DoCast(m_creature->getVictim(),SPELL_FROST_BOLT);
FrostBolt_Timer = (rand()%60)*1000;
}else FrostBolt_Timer -= diff;
//Check for Frost Bolt Nova
if (FrostBoltNova_Timer < diff)
{
DoCast(m_creature->getVictim(),SPELL_FROST_BOLT_NOVA);
FrostBoltNova_Timer = 15000;
}else FrostBoltNova_Timer -= diff;
//Check for Chains Of Kelthuzad
if (ChainsOfKelthuzad_Timer < diff)
{
//DoCast(m_creature->getVictim(),SPELL_CHAINS_OF_KELTHUZAD);
//if (rand()%2)
//DoScriptText(SAY_CHAIN1, m_creature);
//else
//DoScriptText(SAY_CHAIN2, m_creature);
ChainsOfKelthuzad_Timer = (rand()%30+30)*1000;
}else ChainsOfKelthuzad_Timer -= diff;
//Check for Mana Detonation
if (ManaDetonation_Timer < diff)
{
DoCast(m_creature->getVictim(),SPELL_MANA_DETONATION);
if (rand()%2)
DoScriptText(SAY_SPECIAL1_MANA_DET, m_creature);
ManaDetonation_Timer = 20000;
}else ManaDetonation_Timer -= diff;
//Check for Shadow Fissure
if (ShadowFisure_Timer < diff)
{
DoCast(m_creature->getVictim(),SPELL_SHADOW_FISURE);
if (rand()%2)
DoScriptText(SAY_SPECIAL3_MANA_DET, m_creature);
ShadowFisure_Timer = 25000;
}else ShadowFisure_Timer -= diff;
//Check for Frost Blast
if (FrostBlast_Timer < diff)
{
DoCast(m_creature->getVictim(),SPELL_FROST_BLAST);
if (rand()%2)
DoScriptText(SAY_FROST_BLAST, m_creature);
FrostBlast_Timer = (rand()%30+30)*1000;
}else FrostBlast_Timer -= diff;
//start phase 3 when we are 40% health
if (!Phase3 && (m_creature->GetHealth()*100 / m_creature->GetMaxHealth()) < 40)
{
Phase3 = true;
DoScriptText(SAY_REQUEST_AID, m_creature);
//here Lich King should respond to KelThuzad but I don't know which creature to make talk
//so for now just make Kelthuzad says it.
DoScriptText(SAY_ANSWER_REQUEST, m_creature);
}
if (Phase3 && (GuardiansOfIcecrown_Count < 5))
{
if (GuardiansOfIcecrown_Timer < diff)
{
//Summon a Guardian of Icecrown in a random alcove (Creature # 16441)
//uint32 TimeToWalk;
Creature* pUnit = NULL;
float Walk_Pos_X;
float Walk_Pos_Y;
float Walk_Pos_Z;
switch(rand()%6)
{
case 0:
pUnit = m_creature->SummonCreature(16441,ADDX_LEFT_FAR,ADDY_LEFT_FAR,ADDZ_LEFT_FAR,ADDO_LEFT_FAR,TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT,1000);
//Setting walk position
Walk_Pos_X = WALKX_LEFT_FAR;
Walk_Pos_Y = WALKY_LEFT_FAR;
Walk_Pos_Z = WALKZ_LEFT_FAR;
break;
case 1:
pUnit = m_creature->SummonCreature(16441,ADDX_LEFT_MIDDLE,ADDY_LEFT_MIDDLE,ADDZ_LEFT_MIDDLE,ADDO_LEFT_MIDDLE,TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT,1000);
//Start moving guardian towards the center of the room
Walk_Pos_X = WALKX_LEFT_MIDDLE;
//.........这里部分代码省略.........
示例9: maths_rand
double maths_rand(double a, double b)
{
return (b - a) * ((double)rand() / RAND_MAX) + a;
}
示例10: main
int main(int argc, char *argv[])
{
srand(time(NULL));
bool giveme = false;
long zero = 0;
char buf[100];
bool gotflag = false;
puts("Welcome to hell, we have real strong JumpFu here");
start:
DBUG("start")
puts("Where to?");
fflush(0);
zero = 0;
fgets(buf, 117, stdin);
buf[99] = 0;
if (strncmp(buf+10, "WHO GOES THERE", strlen("WHO GOES THERE")) == 10)
goto six;
else
goto two;
one:
DBUG("one")
puts("WELCOME BACK");
goto start;
two:
DBUG("two")
puts("Who are you? Where are you even going?");
fflush(0);
if (buf[69]=='8')
goto eleven;
if (buf[69]=='9')
goto five;
goto one;
three:
DBUG("three")
puts("Here we go again...");
fflush(0);
goto eight;
four:
DBUG("four")
if (!zero)
goto nine;
five:
DBUG("five")
giveme = true;
if (!zero)
goto eight;
six:
DBUG("six")
giveme = false;
seven: //lucky number seven
DBUG("seven")
if (giveme){
gotflag = true;
print_flag();
goto end;
}else{
puts("NO!!! GO AWAY");
fflush(0);
}
eight:
DBUG("eight")
puts("Goodnight! See you later!");
fflush(0);
sleep(1);
if (rand() % 2)
goto one;
else
goto twelve;
nine:
DBUG("nine")
goto two;
ten:
DBUG("ten")
if (!zero)
goto end;
goto one;
eleven:
DBUG("eleven")
if (strncmp(&zero, "risky!", strlen("risky")))
goto eight;
goto six;
twelve:
DBUG("twelve")
if (zero)
goto seven;
else
goto ten;
end:
DBUG("end")
if (!gotflag)
puts("Looks like you are leavin here empty handed.");
else
puts("Nice work. You won.");
fflush(0);
return 0;
}
示例11: moveAction
// CONSTRUCTOR FOR BABIES (born creatures)
Creature::Creature(sf::Vector2u& w, Creature* dad, Creature* mum)
: moveAction(w, position)
{
windowSize = &w;
init();
// POSITION OF MUM
position = sf::Vector2f(mum->getPosition().x, mum->getPosition().y);
// INHERIT CHARACTERISTICS FROM MUM OR DAD CREATURE
// SIZE: INTERPOLATION OF MUM AND DAD
int mutationRisk = rand()%100;
size = (dad->getSize() + mum->getSize()) / 2;
if(mutationRisk > 95) size = rand()%5 + 10.f;
// SIGHT: INTERPOLATION OF MUM AND DAD
mutationRisk = rand()%100;
sightRadius = (dad->getSightRadius() + mum->getSightRadius()) / 2;
if(mutationRisk > 95) sightRadius = size + rand()%100;
// COLOR: RANDOM MUM OR DAD
mutationRisk = rand()%100;
body.setFillColor((rand()%2 == 0) ? dad->getColor() : mum->getColor());
if(mutationRisk > 95) body.setFillColor(sf::Color(rand()%255, rand()%255, rand()%255, 200));
// LIFETIME: RANDOM MUM OR DAD
mutationRisk = rand()%100;
timeToLive = (rand()%2 == 0) ? dad->getTTL() : mum->getTTL();
if(mutationRisk > 95) timeToLive = rand()%10000 + 100;
// REPLICATION TIMER: RANDOM MUM OR DAD
mutationRisk = rand()%100;
timeToReplicate = (rand()%2 == 0) ? dad->getTTR() : mum->getTTR();
if(mutationRisk > 95) timeToReplicate = rand()%800 + 200;
// REPLICATION DURATION:INTERPOLATION OF MUM AND DAD
mutationRisk = rand()%100;
replicationDuration = (dad->getReplicationDuration() + mum->getReplicationDuration()) / 2;
if(mutationRisk > 95) replicationDuration = rand()%1000 + 200;
}
示例12: random
int random(int a,int b) //returns a pseudo random integer between a and b
{
int d=b-a;
return a+rand()%d;
}
示例13: UpdateAI
void UpdateAI(const uint32 diff)
{
if (!UpdateVictim())
return;
if (Assassins_Timer)
{
if (Assassins_Timer <= diff)
{
SpawnAssassin();
Assassins_Timer = 0;
}
else Assassins_Timer -= diff;
}
if (InBlade)
{
if (Wait_Timer)
{
if (Wait_Timer <= diff)
{
if (target_num <= 0)
{
// stop bladedance
InBlade = false;
me->SetSpeed(MOVE_RUN, 2);
(*me).GetMotionMaster()->MoveChase(me->getVictim());
Blade_Dance_Timer = 30000;
Wait_Timer = 0;
if (HeroicMode)
Charge_timer = 5000;
}
else
{
//move in bladedance
float x, y, randx, randy;
randx = float(rand() % 40);
randy = float(rand() % 40);
x = 210 + randx ;
y = -60 - randy ;
(*me).GetMotionMaster()->MovePoint(1, x, y, me->GetPositionZ());
Wait_Timer = 0;
}
}
else Wait_Timer -= diff;
}
}
else
{
if (Blade_Dance_Timer)
{
if (Blade_Dance_Timer <= diff)
{
target_num = TARGET_NUM;
Wait_Timer = 1;
InBlade = true;
Blade_Dance_Timer = 0;
me->SetSpeed(MOVE_RUN, 4);
return;
}
else Blade_Dance_Timer -= diff;
}
if (Charge_timer)
{
if (Charge_timer <= diff)
{
DoCast(SelectUnit(SELECT_TARGET_RANDOM, 0), H_SPELL_CHARGE);
Charge_timer = 0;
}
else Charge_timer -= diff;
}
if (Summon_Assistant_Timer <= diff)
{
for (uint32 i = 0; i < summoned; i++)
{
switch (rand() % 3)
{
case 0:
me->SummonCreature(MOB_HEARTHEN_GUARD, AddsEntrance[0], AddsEntrance[1], AddsEntrance[2], 0, TEMPSUMMON_CORPSE_TIMED_DESPAWN, 10000);
break;
case 1:
me->SummonCreature(MOB_SHARPSHOOTER_GUARD, AddsEntrance[0], AddsEntrance[1], AddsEntrance[2], 0, TEMPSUMMON_CORPSE_TIMED_DESPAWN, 10000);
break;
case 2:
me->SummonCreature(MOB_REAVER_GUARD, AddsEntrance[0], AddsEntrance[1], AddsEntrance[2], 0, TEMPSUMMON_CORPSE_TIMED_DESPAWN, 10000);
break;
}
}
if (rand() % 100 < 6) summoned++;
Summon_Assistant_Timer = 15000 + (rand() % 5000) ;
}
else Summon_Assistant_Timer -= diff;
DoMeleeAttackIfReady();
}
if (resetcheck_timer <= diff)
{
float tempx;
tempx = me->GetPositionX();
if (tempx > 255 || tempx < 205)
//.........这里部分代码省略.........
示例14: UpdateAI
void UpdateAI(const uint32 diff)
{
if (!UpdateVictim())
{
if(UnsummonCheck < diff && m_creature->isAlive())
{
m_creature->SetLootRecipient(NULL);
m_creature->SetVisibility(VISIBILITY_OFF);
m_creature->DealDamage(m_creature, m_creature->GetMaxHealth(), NULL, DIRECT_DAMAGE, SPELL_SCHOOL_MASK_NORMAL, NULL, false);
m_creature->RemoveCorpse();
}else UnsummonCheck -= diff;
return;
}
if(Fireball_Timer < diff)
{
if(Unit *victim = SelectUnit(SELECT_TARGET_RANDOM,0))
DoCast(victim, SPELL_FIREBALL,true);
Fireball_Timer = 4000+rand()%3000;
}else Fireball_Timer -= diff;
if(flight) // phase 1 - the flight
{
Creature *Vazruden = Unit::GetCreature(*m_creature,VazrudenGUID);
if(Fly_Timer < diff || !(Vazruden && Vazruden->isAlive() && (Vazruden->GetHealth()*5 > Vazruden->GetMaxHealth())))
{
flight = false;
BellowingRoar_Timer = 6000;
ConeOfFire_Timer = 12000;
m_creature->RemoveUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT + MOVEMENTFLAG_LEVITATING);
m_creature->AddUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
m_creature->GetMotionMaster()->Clear();
if(Unit *victim = SelectUnit(SELECT_TARGET_NEAREST,0))
m_creature->AI()->AttackStart(victim);
DoStartMovement(m_creature->getVictim());
DoScriptText(EMOTE, m_creature);
return;
}else Fly_Timer -= diff;
if(Turn_Timer < diff)
{
uint32 waypoint = (Fly_Timer/10000)%2;
if(m_creature->GetDistance(VazrudenRing[waypoint][0],VazrudenRing[waypoint][1],VazrudenRing[waypoint][2]) > 5)
m_creature->GetMotionMaster()->MovePoint(0,VazrudenRing[waypoint][0],VazrudenRing[waypoint][1],VazrudenRing[waypoint][2]);
Turn_Timer = 10000;
}else Turn_Timer -= diff;
}
else // phase 2 - land fight
{
if(ConeOfFire_Timer < diff)
{
DoCast(m_creature, SPELL_CONE_OF_FIRE);
ConeOfFire_Timer = 12000;
Fireball_Timer = 4000;
}else ConeOfFire_Timer -= diff;
if(HeroicMode && BellowingRoar_Timer < diff)
{
DoCast(m_creature, SPELL_BELLOWING_ROAR);
BellowingRoar_Timer = 45000;
}else BellowingRoar_Timer -= diff;
DoMeleeAttackIfReady();
}
}
示例15: random
float random(float minimum, float maximum)
{
return minimum + static_cast <float> (rand()) / ( static_cast <float> (RAND_MAX/(maximum - minimum)));
}