# Making srand ignore certain values?

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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++?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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]; 

##### Share on other sites
I would just make 2 arrays rather than mixing your special boss enemies with regular enemies.

##### Share on other sites

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];

##### Share on other sites

[quote name='Buckeye' timestamp='1296444007' post='4767288']
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];
[/quote]
You're correct about using numPositions (vs numPositions-1) and RAND_MAX+1.0. Good observation. Why a double vs. float?

##### Share on other sites

[quote name='Buckeye' timestamp='1296444007' post='4767288']
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];
[/quote]

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).

##### Share on other sites

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.

##### Share on other sites

[quote name='Buckeye' timestamp='1296452591' post='4767342']
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.
[/quote]

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.

##### Share on other sites
Thank you, everyone! I'm working on fixing my code now. I appreciate all the support.

##### Share on other sites
Be careful not to confuse rand() and srand(). srand() seeds the RNG, you typically call this once, and rand() gets the next value in the sequence, which you call when you need a random number.