Search code examples
c++srandrandom

rand () for c++ with variables


int userHP = 100;
int enemyHP = rand() % ((userHP - 50) - (userHP - 75)) + 1;

okay, for some reason this doesnt seem to work right, im trying to get 50 -25 hp for enemys.

also id rather it be a percentage... like

int enemyHP = rand() % ((userHP / 50%) - (userHP / 75%)) + 1;

but id like to stick with integers and not mess with floats or doubles... can someone help me?


Solution

  • Perform some algebra on this:

    rand() % ((userHP - 50) - (userHP - 75)) + 1;
    
    rand() % (userHP - 50 - userHP + 75) + 1;
    
    rand() % (userHP - userHP - 50 + 75) + 1;
    
    rand() % (-50 + 75) + 1;
    

    ...and you can quickly see what's going wrong. Why not use doubles?