Matic Posted August 9, 2013 Share Posted August 9, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/ Share on other sites More sharing options...
Solution Muddy_Funster Posted August 9, 2013 Solution Share Posted August 9, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444156 Share on other sites More sharing options...
Matic Posted August 9, 2013 Author Share Posted August 9, 2013 I see this could be a nice probability algorithm but I don't understand this: floor( 10 + (RAND() * 190)) Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444160 Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444163 Share on other sites More sharing options...
Matic Posted August 9, 2013 Author Share Posted August 9, 2013 yes but where in there is the encounter probability? It looks jut like a random selection on a first glance Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444177 Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 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 goblin goblin golem bugbear goblin dragon goblin bugbear bugbear 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 Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444181 Share on other sites More sharing options...
Matic Posted August 9, 2013 Author Share Posted August 9, 2013 Thanks so much for help! Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444189 Share on other sites More sharing options...
Muddy_Funster Posted August 9, 2013 Share Posted August 9, 2013 You're very welcome Quote Link to comment https://forums.phpfreaks.com/topic/280990-influence-appearance-chance/#findComment-1444195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.