random array percent
Posted: Sun Jan 27, 2008 12:54 pm
I was wondering if someone could help me with making a random array with percent
(random array is chosen due to a percent chance that it has)
in $randomdrop2 is the percentages. i am coding an rpg in which a monster drops an item. currently it will just pick out of a list but i would like to add some percentages to the list so we can have rare drops.
the sample code will pick a random array and then will randomly display a "good" if the percent is high enough. problem with this is i would like it to always pick a random array.
example would be
apple - common
sword - rare
the apple would have more of a chance to be chosen yet the sword can still appear.
idonno if i went on too much of an explaining rant but oh well, if anyone could help that would be awesome!!
Thanks, Sled
(random array is chosen due to a percent chance that it has)
Code: Select all
$randomdrop = "1,2,3,4,5,6";
$randomdrop2 = "10,80,50,60,30,40";
$a = explode(",",$randomdrop);
$b = explode(",",$randomdrop2);
mt_srand((double)microtime()*1000000);
$k = array_keys($a);
$r = mt_rand(0, count($k)-1);
echo $a[$r]." %".$b[$r];
$percent=$b[$r]/10;
if (rand($percent,10) == 10)
{echo $a[$r]." good!";}
the sample code will pick a random array and then will randomly display a "good" if the percent is high enough. problem with this is i would like it to always pick a random array.
example would be
apple - common
sword - rare
the apple would have more of a chance to be chosen yet the sword can still appear.
idonno if i went on too much of an explaining rant but oh well, if anyone could help that would be awesome!!
Thanks, Sled