Help with improving my random generator
Posted: Wed Mar 25, 2009 3:16 pm
Hey guys. I have here a random script-generator that have priorities to them. What I would like to do, is add a value to each item, while keeping the priorities but still keeping it somewhat random. So if "havregyn" have the value of 50 and and "eggknock" have the value of 60, those two won't be able to spawn together because the maximum value has to be between 90 and 100. But "havregyn" and "bananasplit" (with a value of 25) and " " (with a value of 20) would be able to spawn together. How would I go about doing that? I hope you guys understand. Thanks 
Code: Select all
<?php
function dodosrandgen() {
// set number of links you want to show at once
$show_link = 5;
// separate the links by, i.e. comma would be ",";
$separator = " ";
// Add links
// Please make sure you number entries CORRECTLY
// Look at the example for reference!
// DO NOT erase the priority part, leave all
// to 1 if you don't want them to have different
// priorities!
$links[0][link] = "havregyn</br>";
$links[0][priority] = 25;
$links[1][link] = "helse</br>";
$links[1][priority] = 1;
$links[2][link] = "oatmeal</br>";
$links[2][priority] = 1;
$links[3][link] = "eggknock</br>";
$links[3][priority] = 1;
$links[4][link] = "";
$links[4][priority] = 15;
$links[5][link] = "";
$links[5][priority] = 15;
$links[6][link] = "green beans</br>";
$links[6][priority] = 60;
$links[7][link] = "bananasplit</br>";
$links[7][priority] = 40;
// HERE COMES THE BODY
if($links) {
for($i = -1; $i < count($links); $i++) {
for($j = 0; $j < $links[$i][priority]; $j++) {
$newlinksindex = count($newlinks) + 1;
$newlinks[$newlinksindex] = $links[$i][link];
}
}
} // end of if link exist
// debug
if($show_link >= count($newlinks))
$show_link = count($newlinks);
$showarray[0] = "";
$showindex = 0;
// Select a random element from the new array
do {
$randindex = rand(0, count($newlinks));
if(!in_array($newlinks[$randindex], $showarray) && $newlinks[$randindex] != "") {
$showarray[$showindex] = $newlinks[$randindex];
//print_r($showarray);
$showindex++;
}
}while(count($showarray) <= $show_link);
// finally print out
for($i = 0; $i < $show_link; $i++ ) {
if($show_link == 1 || (($i + 1) == $show_link))
print $showarray[$i];
else
print $showarray[$i].$separator;
}
//print_r($ranarray);
//print_r($links);
//print_r($newlinks);
} // end of the dodosrandgen function
?>