• Create Account

## Making srand ignore certain values?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1RezonAce  Members

Posted 30 January 2011 - 08:05 PM

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

### #2SiCrane  Moderators

Posted 30 January 2011 - 08:20 PM

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.

### #3mrchrismnh  Members

Posted 30 January 2011 - 08:24 PM

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.

"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

### #4Buckeye  GDNet+

Posted 30 January 2011 - 09:20 PM

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


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #5nobodynews  Members

Posted 30 January 2011 - 10:03 PM

I would just make 2 arrays rather than mixing your special boss enemies with regular enemies.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

### #6Álvaro  Members

Posted 30 January 2011 - 11:34 PM

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

### #7Buckeye  GDNet+

Posted 30 January 2011 - 11:43 PM

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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #8way2lazy2care  Members

Posted 31 January 2011 - 08:25 AM

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

### #9frob  Moderators

Posted 31 January 2011 - 10:54 AM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #10Álvaro  Members

Posted 31 January 2011 - 03:08 PM

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.

### #11RezonAce  Members

Posted 01 February 2011 - 04:02 PM

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

### #12kbw  Members

Posted 01 February 2011 - 04:27 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.