PHP Maths

  • Nebulous
  • Beginner
  • Beginner
  • Nebulous
  • Posts: 59
  • Loc: Hampshire, UK

Post 3+ Months Ago

Hey,

I am making a game in PHP which relies heavily on chance.

For the chance, I have worked it out using the following:

- Each record in the database has a fail rate (standard integer).
- I then just rand() to generate a number between 1 and the fail rate.
- If the random number is 1, you fail.
- I then work out the percentage for this. e.g. a fail rate of 2 would be 50% as its either 1 or 2.

The problem I am now faced with is that depending on factors that change during the process of the game, the fail rate changes....

The problem I am having, is adding a 10% increase to the fail rate. SO 50% would become 55% and NOT 60%.

Its too early in the morning, after a long weekend for my brain to function correctly. Any assistance on this would be great, or a alterntitive way of generating the whole thing.

Regards.

Dave.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Limotek
  • Newbie
  • Newbie
  • Limotek
  • Posts: 5

Post 3+ Months Ago

The way you're doing it seems ok but makes it difficult to change the fail rates afterwards.

I would personally change the database field to store the percentage i.e. an integer between 0-100.

You can then rand(100) and if the value returned is below the database fail rate percentage, you return a fail.

Doing it this way allows you to easily increase the fail rates by 10% just mutiplying the database fields by 1.1.
  • Nebulous
  • Beginner
  • Beginner
  • Nebulous
  • Posts: 59
  • Loc: Hampshire, UK

Post 3+ Months Ago

Ah, thank you - that seems a much simpler way of doing things.

I shall give that a go.


Regards.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 92 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.