Page 1 of 1

Using a function that selects random values [solved]

Posted: Mon Oct 31, 2011 2:49 am
by MikeD
Yep, another problem haha. (Sorry guys, I am a noob)

Everything was going smooth on my random loot generator, until this.

Basically I am trying to get multiple random stats from this array

Code: Select all

$itemstats = array("speed","power","melee","meleedef","health","agility");
Using this function from google.

Code: Select all

function Select_Random_Indices($itemstats, $count = 2)
{
    if($count > 0)
    {
        if($count == 1)
        {
            $result = array(array_rand($itemstats, $count));
        }
        else
        {
            $result = array_rand($itemstats, $count);
        }
    }
    else
    {
        $result = array();
    }

    return $result;
} 
function Select_Random_Entries($itemstats, $count = 2)
{
    $result = array();
    $index_array = Select_Random_Indices($itemstats, $count);

    foreach($index_array as $index)
    {
        $result[$index] = $itemstats[$index];
    }

    return $result;
}
So, my two part question is..How exactly does that function work, and how do I get the results of it? I've only used functions 1 other time, so I am very unfamiliar with them.

Re: Using a function that selects random values

Posted: Mon Oct 31, 2011 5:16 am
by Ark

Code: Select all

<?php
function randomStats($array, $counter) #<== $array:put the array variable.... counter: how many "random-stats" you want.
{
$arraysize=sizeof($array)-1;   #<== get the length of the array.
     for($i=0;$i<$counter;$i++)
     {
          echo $array[rand(0,$arraysize)];   #<== this is just for echoing out.  the function below will tell you how to return multiple values.
     }
}

function returnRandomStats($array, $counter)
{
    $arraysize=sizeof($array)-1;   #<== get the length of the array.
     for($i=0;$i<$counter;$i++)
     {
          $return_newarray[$i]= $array[rand(0,$arraysize)];   
     }
return $return_newarray;
}
?>
Now how to use them?!

Code: Select all

<?php
$test = array('one','two','three');
 
randomStats($array,5);
?>
it will output like...
two
one
two
three
two
(just an example)

now to return those as different variables...

Code: Select all

<?php
$test = array('one','two','three');
list($one,$two,three,$four,$five) = returnRandomStats($test,5); #<== the list key is to return an array in a function.

# to verify if exists
echo $one ."<br />";
echo $two ."<br />";
echo $three ."<br />";
echo $four ."<br />";
echo $five ."<br />";

?>
it will output...

one
three
two
three
one

Hope that's what you needed.

Re: Using a function that selects random values

Posted: Mon Oct 31, 2011 5:44 pm
by MikeD
Yes this is awesome, thanks a ton..However I am having trouble with it.

It isn't pulling 2 stats at random. It is pulling Speed and Speed. or with the $test array you made, $one,$two,$three,$four,$five are all one.

I need it to pull 2 different stats.

Re: Using a function that selects random values

Posted: Mon Oct 31, 2011 11:24 pm
by MikeD
I got it pulling random stats now. However often times it will pull 2 of the same stat.

I added this unset($itemstats[$return_newarray[$i]]);

That isn't working, it's still coming up with duplicates :(

Re: Using a function that selects random values

Posted: Tue Nov 01, 2011 2:36 am
by Ark
Ok... So you don't want it to repeat the same stat??

Code: Select all

<?php
function randomStats($array, $counter) #<== array: the array variable.. counter:how many "random-stats" variables you want.
{
    shuffle($array);
    for($i=0;$i<$counter;$i++)
    {
        $newarray[$i]=$array[$i];
    }
    
return $newarray;
}

$itemstats = array("speed","power","melee","meleedef","health","agility");
list($stat1,$stat2)=randomStats($itemstats,2);
echo $stat1."<br>";
echo $stat2."<br>";
?>
just use the shuffle() function :)

In that example it will always return two different stats. ;)

took me more than an hour to figure it out :roll:

Re: Using a function that selects random values[solved]

Posted: Tue Nov 01, 2011 3:07 am
by MikeD
Wow, thank you! I can't believe it was something that simple. I've been trying different things all day long, sigh.

Thanks again to both of you!

Re: Using a function that selects random values[solved]

Posted: Thu Nov 03, 2011 1:44 am
by Jackolantern
MikeD wrote:Wow, thank you! I can't believe it was something that simple. I've been trying different things all day long, sigh.

Thanks again to both of you!
Glad you got it fixed! However, you have to edit the initial post with [solved] to change the actual thread name. I will do it for you :)