Page 1 of 1

Randomizing monster spawns [SOLVED]

Posted: Tue Nov 15, 2005 8:31 pm
by Moocat
When randomizing my monster spawns, I pull a list of monsters with chances for them to spawn (which add up to 100%). For some reason I'm just running into a brain block as to how to pull them out. Here is an example:

Red Slime = 5% chance to spawn
Small Snake = 20% chance to spawn
Rat = 25% chance to spawn
Green Slime = 50% chance to spawn

And for the life of me I just can't think of how to randomize which one will spawn :p

Posted: Tue Nov 15, 2005 8:41 pm
by trukfixer
put em in an associative array and use array_rand perhaps?

Re: Randomizing monster spawns

Posted: Tue Nov 15, 2005 8:42 pm
by Charles256
Moocat wrote:When randomizing my monster spawns, I pull a list of monsters with chances for them to spawn (which add up to 100%). For some reason I'm just running into a brain block as to how to pull them out. Here is an example:

Red Slime = 5% chance to spawn
Small Snake = 20% chance to spawn
Rat = 25% chance to spawn
Green Slime = 50% chance to spawn

And for the life of me I just can't think of how to randomize which one will spawn :p
red = 1
small=2222
rat=33333
green=4444444444
store those numbers in an array..
pick a random number from the array...
maybe? I could be being retarded too..

Posted: Tue Nov 15, 2005 8:46 pm
by jabbaonthedais
The only thing I can think of is shuffling numbers 1-20, and have each of those numbers assigned to a monster. Each number would represent 5%, so Red Slime would be stored to one number, where Green Slime would be stored to 10 variables, like $monster[1] and $monster[2]. There is probably a much better way to do this, but this would work.

Re: Randomizing monster spawns

Posted: Tue Nov 15, 2005 8:47 pm
by jabbaonthedais
Charles256 wrote: red = 1
small=2222
rat=33333
green=4444444444
store those numbers in an array..
pick a random number from the array...
maybe? I could be being retarded too..
Thats a creative way to do it, and should work great.

Posted: Tue Nov 15, 2005 8:49 pm
by Charles256
it's the only way I've ever seen:-D seem to remember this being asked in regard to songs one time....

Posted: Tue Nov 15, 2005 8:51 pm
by Dm7

Code: Select all

<?php
//creates a random number... 1 to 100 (as in 1% to 100%)
$rand = rand(1, 100);

// checks if it's 5 or less (creating 5% of it)
if ($rand <= 5) 
{
	echo "Congrats, you got a Red Slime!";
} 
// checks if there's 25 or less and is greater than 5 (creating 20% of it)
elseif ($rand > 5 && $rand <= 25)
{
	echo "Congrats, you got a Small Snake!";
}
// checks if there's 50 or less and is greater than 25 (creating 25% of it)
elseif ($rand > 25 && $rand <= 50)
{
	echo "Congrats, you got a Rat!";
}
// since the rest is 50%, then else statement is used
else
{
	echo "Congrats, you got a Green Slime!";
}
?>
It has been tested... that's how I would do it.. maybe there's a better way to do it. :) But from what I can see, it seems to work just fine.

Posted: Tue Nov 15, 2005 9:38 pm
by Moocat
Alright, let's see if I got this down right:

Code: Select all

srand();
//SQL grab real monster array here
$monster_array = array("6:25", "7:20", "5:5");
$number_monster = count($monster_array);
//Subtract one because arrays start with 0
$number_monster--;
//Loop through all unique monsters, there will be no duplicates so no worries
while($number_monster >= 0) {
	//Split monster stats into monster ids and the percent chance to spawn
	$monster_stats = explode(":", $monster_array[$number_monster]);
	$monster_id = $monster_stats[0];
	$percent = $monster_stats[1];
	//Assign the same number of monster ids to the array as the percent number (will add to 100 later on, I know it's only 50 now)
	while($percent > 0) {
		$monster_count[] = $monster_id;
		$percent--;
	}
	$number_monster--;
}
//Set the monster id

$monster_spawned = array_rand($monster_count);
$monster_spawned = $monster_count[$monster_spawned];

//SQL select monster stuff goes here

echo "Oh no, you've encountered a ", $monster_spawned;
This appears to work :) Posted here just to let you smart people check my work over. Also, I thought you didn't need srand() for seeding anymore? I needed it and I have PHP 4.3, otherwise it just returned 6 over and over :/