Jump to content

Numbers into % from an array.


Michdd

Recommended Posts

I'm currently using something like this:

 

$monster[1] = "spirit";
$times[1] = "18";
$monster[2] = "golden spirit";
$times[2] = "18";
$monster[3] = "hank";
$times[3] = "15";
$monster[4] = "brown fluffy";
$times[4] = "40";

foreach ($monster as $index => $value)
{
    $array = array_merge($array, array_fill(0, $times[$index], $value));
}

$monster = $array[array_rand($array)];

 

Now that puts the times of $times[$i] into the array then picks a random one and stores it into $monster. Now the more the times for each monster obviously the higher the % of it being picked. But I would like it to have a way that I could set the variables to %chance instead of the times to put it into an array. Is there like a way that I can make it put a certain amount by putting a number like "10" and it'll generate a number equal to one that by putting it into the array that many times gives it that 10% chance.

Link to comment
https://forums.phpfreaks.com/topic/133006-numbers-into-from-an-array/
Share on other sites

why repeat post?

 

see this other post by you:

http://www.phpfreaks.com/forums/index.php/topic,225761.msg1040575.html#msg1040575

 

putting percentages in for the values in my (second) solution there will function exactly like you describe. 10 will be 10%, etc (assuming all of the percentages add up to 100)

why repeat post?

 

see this other post by you:

http://www.phpfreaks.com/forums/index.php/topic,225761.msg1040575.html#msg1040575

 

putting percentages in for the values in my (second) solution there will function exactly like you describe. 10 will be 10%, etc (assuming all of the percentages add up to 100)

It's different. I mean something that will do this:

 

Say I have enter 2 things:

 

$percent['mosnter1'] = "10";

$percent['monster2'] = "90";

 

So then by that I want it to output something that will put the least amount of possible entries into the array that will output monster1 10% of the time, and monster 2 %90 of the time.

It is the same, just change a few variable names.. you'll need to learn how code works and how to adapt it if you hope to avoid frustrating people in the future :)

 

<?php
$percent['monster1'] = 10;
$percent['monster2'] = 90;

$rand = mt_rand(1, array_sum($percent));
$timeSum = 0;
foreach($percent as $key => $value) {
   $timeSum += $value;
   if($rand <= $timeSum) {
      $monster = $key;
      break;
   }
}

echo $monster."\n";
?>

It is the same, just change a few variable names.. you'll need to learn how code works and how to adapt it if you hope to avoid frustrating people in the future :)

 

<?php
$percent['monster1'] = 10;
$percent['monster2'] = 90;

$rand = mt_rand(1, array_sum($percent));
$timeSum = 0;
foreach($percent as $key => $value) {
   $timeSum += $value;
   if($rand <= $timeSum) {
      $monster = $key;
      break;
   }
}

echo $monster."\n";
?>

So, that won't put 10 monster1's into the database and 90 monster2's but rather put whatever makes it true so that it's %10 : %90 odds?

It doesn't create a huge array of 100 monsters with the vast majority of them being duplicates, no, that's terribly inefficient in both speed and memory. This in effect creates value ranges for each monster and generates a random number in that range.

 

In this case monster1 is "assigned" 1-10 (10% of the numbers) and monster2 is "assigned" the range 11-100 (90% of the numbers). I then generate a random number between 1-100 and it, in effect, selects the monster in that value range. So if mt_rand() returns a 27 it would select monster2 because it falls in that range.

 

If you want me to explain how it works exactly, feel free to ask! I'd be completely willing to go into detail of how it works if you don't understand.

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.