Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Making srand ignore certain values?

4: Adsense

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.


  • You cannot reply to this topic
11 replies to this topic

#1 RezonAce   Members   

101
Like
0Likes
Like

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

#2 SiCrane   Moderators   

11763
Like
0Likes
Like

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.

#3 mrchrismnh   Members   

82
Like
0Likes
Like

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


#4 Buckeye   GDNet+   

10743
Like
0Likes
Like

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.


#5 nobodynews   Members   

3094
Like
3Likes
Like

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   

20922
Like
0Likes
Like

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


#7 Buckeye   GDNet+   

10743
Like
0Likes
Like

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.


#8 way2lazy2care   Members   

786
Like
0Likes
Like

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

#9 frob   Moderators   

44033
Like
0Likes
Like

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   

20922
Like
0Likes
Like

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.

#11 RezonAce   Members   

101
Like
0Likes
Like

Posted 01 February 2011 - 04:02 PM

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

#12 kbw   Members   

88
Like
0Likes
Like

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.