Using a function that selects random values [solved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Using a function that selects random values [solved]

Post 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.
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Using a function that selects random values

Post 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.
Orgullo Catracho
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Using a function that selects random values

Post 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.
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

Re: Using a function that selects random values

Post 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 :(
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Using a function that selects random values

Post 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:
Orgullo Catracho
User avatar
MikeD
Posts: 294
Joined: Thu Sep 08, 2011 4:28 am

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

Post 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!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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 :)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”