简体   繁体   中英

Using variable probability to affect the output of rand()

I am having a hard time wrapping my head around this concept. I apologize for the vague subject line. I'm in the mists of creating a text base RPG and am stuck on the battle sequence. I have some variables that are effected, but the strength aspect is whats confusing.

this is the rule of what happens with strength:

Strength increases or decreases by 5 depending if win or lose. 

I'm looking to have a 50/50 chance of winning or losing, but always at least a 5% chance of losing to make the game fair. Strength is an added bonus to the probability of winning as well.

My question:

How can I use strength to benefit the random number generated to win or lose? 

It will run and will be 50/50 but once strength gets to be around 25-30 it wins 100% of the time making it unfair.I need the results to be 50/50 with the bonus of strength to be applied.

I know probably super simple but I can't figure it out. Please let me know if I need to explain better. This is kind of a lot to jump into for a learning project, I appreciate any and all assistance.

My code below is the snippet of the calculation. I know it might be confusing or totally wrong but even a slight nudge in the right direction would be huge. Thanks everyone!

 randomNum = 1 + rand() % (100 - 1 + 1);
    randomNum = player->strength + randomNum;
    if (randomNum >= 51){//changed to be based off rand() + strength
        player->money += 50;
        if (player->strength < 95){//made it to not go above 95
            player->strength += 5;
        }
printf("\n You won $50 and 5 strength points!\n\n ");

else{
        if (randomNum <= 50){//changed if less than 50 you lose
        {
        player->strength -= 5;
        }

printf("\n You lost a life and 5 strength points!\n\n ");

In RPGs with explicit die rolls you usually have an 'attacking' score (strength in your case) that is pitted against a 'defending' score (defence, resistance, fortitude, whatever).

The values are usually chosen such that an attack succeeds if attack + die >= defence (leaving aside modifiers and such), with the added proviso that very low rolls - 1 on a d20, <= 5 on a d100 - always miss and very high rolls - 20 on a d20, > 95 on a d100 - always hit. That would roughly correspond to your 5% nudge.

The difference between defence and attack is the window into which the die rolls. If attack is exactly one half die below defence then the chance of an attack succeeding is 50%. Increasing attack (by increasing strength) reduces the probability of failure, until only automatic misses remain since they are independent of attack/defence scores.

In your case the defence score is 50 but strength can go much higher than that, so that the player can never lose since you don't have automatic misses. Balance-wise it should be such that an average attack is 50 below an average defence, meaning that the hit rate can go below 50% if attack is sub-par or defence is above average. Put base defence at 100 and base attack (strength) at 50, to leave room for things to go in either direction.

For something simple you can try:

int randomNum = 1 + (rand() % 100); //1-100
int testStrength = strength; // 1-100

if (testStrength > 95)
   testStrength = 95;
else if (testStrength < 5)
   testStrength = 5;

   //chance to lose is 5-95% depending on strength
if (randomNum > testStrength) 
{
  //you lose!
}
else
{
  //you win!
}

Note that doing rand() % X won't necessarily give you a perfectly random distribution but may be good enough for a simple game (see here for a solution).

I would start by creating all the rules of your game system on paper. List all the stats, what they affect, the combat rules etc... and then translate that into code. You can look at existing game rules (like D&D or computer RPGs) for basic ideas on some rule systems. Chances are creating a "good" rule system will be the hard part...converting it to code should be easy.

Another thing you can do is test your game rule codes by running through it 100s or 1000s of times and accumulate the results (win/loss in this case). Plot or tabulate the data to make sure it makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM