Randomic func for RPG battle system
Hi you all.
This WAS my func :
int iGetRandom(int iMax,int iDif)
{
int temp=0;
if (!iMax) return 0;
srand(rand());
while ((temp < iMax - iDif) // (temp > iMax))
temp = rand();
return temp;
}
this is WRONG, dont try it! (yes it works ,but bad)
Thanks to Kylotan i got the perfect line :
temp = (int)((double)rand() / ((double)RAND_MAX + 1) * iDif) + (iMax - iDif);
Does Anyone have a new idea ?
srand(rand());
Hmm, i think that part is kinda bad. you should do a
srand((unsigned int) (time(&t)));
somewhere (not in the func, and only once)
where t is a time_t
========================
Game projects:
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
the most usual random function i see/use is
int Random(int min, int max)
{
return((rand()%(max-min+1)+min);
}
you could also do the thing with converting to double and using RAND_MAX
of course, it is usually preceded by srand(timeGetTime());
int Random(int min, int max)
{
return((rand()%(max-min+1)+min);
}
you could also do the thing with converting to double and using RAND_MAX
of course, it is usually preceded by srand(timeGetTime());
Get off my lawn!
Jonatan, it''s better to pass NULL to time() rather than a time_t since you''re only going to use time()''s return value (srand(time(NULL)). This is better because you don''t have to waste valuable memory on a time_t
/. Muzzafarath
/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement