Jump to content

Influence appearance chance


Matic

Recommended Posts

Hey there, using this mysql string:

"SELECT name FROM monsters ORDER BY RAND() LIMIT 1"

I retrieve a random monster encounter from my database. I have a question though, how would one influence the apperance rate of particular rows from my table? In practices this means that some monsters would appear more, some less.

Link to comment
https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/
Share on other sites

you would need to store an "encounter rate" value against each monster, and then apply some math to increase the chance of the number generated applying to the monster with the higher encounter rate.

 

assuming something like :

 

golem : encRT = 50;

goblin : encRT = 200;

 

then a query like :

 

SELECT name FROM monsters WHERE encRT >= floor( 10 + (RAND() * 190)) ORDER BY RAND() LIMIT 1

 

That's just a really basic example to show what I 'm talking about

FLOOR()  -> Return the largest integer value not greater than the argument

10 -> starting point - lowest possible integer value

+(RAND() * 190)  -> generate random value upto starting point (10) + 190 = 200

 

Thus -> FLOOR(10+(RAND()*190)) = create a random integer number between 10 and 200

 

That make sense?

you would need to set that in the database table, choosing values relative to how often you would want the encounter to match the random lookup.

 

e.g

 

TABLE monster:

name | varchar(255) |

encRT | int(5) UNSIGNED

 

name       |  encRT

goblin     |  200

golem      |   50

dragon     |   21

bugbear    |  150

 

QUERY

SELECT name FROM monster WHERE encRT >= FLOOR(10+(RAND()*190)) ORDER BY RAND() LIMIT 1

 

RESULTS

  1. goblin
  2. goblin
  3. golem
  4. bugbear
  5. goblin
  6. dragon
  7. goblin
  8. bugbear
  9. bugbear
  10. dragon

I sampled 10 run throughs, and other than the dragon coming in more than the golem (just the joys of random numbers) it's pretty much the expected return

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.