Jump to content

Influence appearance chance


Matic
Go to solution Solved by Muddy_Funster,

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
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.