Making srand ignore certain values?
I'm trying to make an RPG game (my first project), but I want to make the srand function skip certain values in my enemy array so I can have certain enemies not come up as random encounters,but rather boss fights at fixed points. Is it possible to do this in C++?
All rand() and friends do is generate a series of (more or less) random numbers. If you want any higher logic you have to implement it yourself.
You can srand() % range but you cannot make srand skip values. You can skip them yourself, either bump the return value up to the next eligible encounter or call srand() again, though calling it again could introduce more randomness into your system than you'd want.
Sounds like you want to implement a fixed set of positions chosen at random. If so, try something like:
float frand = float(rand())/RAND_MAX; // frand in range 0-1
int choice = int( frand*(numPositions-1) );
vector position = bossPositions[choice];
I would just make 2 arrays rather than mixing your special boss enemies with regular enemies.
Sounds like you want to implement a fixed set of positions chosen at random. If so, try something like:
float frand = float(rand())/RAND_MAX; // frand in range 0-1
int choice = int( frand*(numPositions-1) );
vector position = bossPositions[choice];
That code doesn't seem right: It would pick the last choice very infrequently. I think this should do the right thing:
double drand = rand()/(RAND_MAX+1.0); // drand in range [0,1)
int choice = int(drand*numPositions);
vector position = bossPositions[choice];
'Buckeye' said:
Sounds like you want to implement a fixed set of positions chosen at random. If so, try something like:
float frand = float(rand())/RAND_MAX; // frand in range 0-1
int choice = int( frand*(numPositions-1) );
vector position = bossPositions[choice];
That code doesn't seem right: It would pick the last choice very infrequently. I think this should do the right thing:
double drand = rand()/(RAND_MAX+1.0); // drand in range [0,1)
int choice = int(drand*numPositions);
vector position = bossPositions[choice];
You're correct about using numPositions (vs numPositions-1) and RAND_MAX+1.0. Good observation. Why a double vs. float?
'Buckeye' said:
Sounds like you want to implement a fixed set of positions chosen at random. If so, try something like:
float frand = float(rand())/RAND_MAX; // frand in range 0-1
int choice = int( frand*(numPositions-1) );
vector position = bossPositions[choice];
That code doesn't seem right: It would pick the last choice very infrequently.
I think this should do the right thing:
double drand = rand()/(RAND_MAX+1.0); // drand in range [0,1)
int choice = int(drand*numPositions);
vector position = bossPositions[choice];
This may be wrong, but it seems like there is a lot of code for something pretty trivial, and it doesn't seem to deal with the OP's problem.
// choice in range [0, NUM_BASIC_ENEMIES) if you want (0, NUM_BASIC_ENEMIES)
//change to srand(PUTYOURSEEDHERE)%NUM_BASIC_ENEMIES+1)
int choice = (srand(PUTYOURSEEDHERE)%NUM_BASIC_ENEMIES)+1;
Enemy chosenEnemy = enemyChoices[choice];
//All enemies up to NUM_BASIC_ENEMIES are basic. Add your bosses to array indices after NUM_BASIC_ENEMIES
Since I appear to have a little more spare time I can elaborate a decent way to do it.
You can save your enemy indices in an enum like this :
enum{
BASIC_ENEMY_ONE,
BASIC_ENEMY_TWO,
NUM_BASIC_ENEMIES,
SUPER_BOSS_ENEMY
}EnemyId;
then you store your enemy definitions in an array with indices corresponding to their EnemyId:
Enemy BasicEnemies[NUM_BASIC_ENEMIES];
BasicEnemies[BASIC_ENEMY_ONE] = //however you define your enemies ;
etc.
Then randomly choose a basic enemy:
int choice = (srand(PUTYOURSEEDHERE)%NUM_BASIC_ENEMIES);
Enemy chosenEnemy = enemyChoices[choice];
You could separate your Boss and Basic enemies further by storing their ids in separate enums and separate arrays (probably what I'd be inclined to do).
You're correct about using numPositions (vs numPositions-1) and RAND_MAX+1.0. Good observation. Why a double vs. float?
At a guess, it is because of the minimum precision of the numbers.
Generally a float has 24 bits of precision, a double has 53 bits.
Compare to the RAND_MAX constant that must be at least 15 bits (0x7fff), but many systems use a 31-bit value (0x7fffffff) for the constant.
'Buckeye' said:
You're correct about using numPositions (vs numPositions-1) and RAND_MAX+1.0. Good observation. Why a double vs. float?
At a guess, it is because of the minimum precision of the numbers.
Generally a float has 24 bits of precision, a double has 53 bits.
Compare to the RAND_MAX constant that must be at least 15 bits (0x7fff), but many systems use a 31-bit value (0x7fffffff) for the constant.
That's right. In many systems rand() returns more bits than what the float will keep, and if numPositions is large, using a float can result in some numbers being significantly more likely than others. This always happens to some degree, but using `double' may lessen the effect in some systems.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement