Archived

This topic is now archived and is closed to further replies.

RodrigoGroff

Randomic func for RPG battle system

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites