Helm me with a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pri_4all
Forum Newbie
Posts: 4
Joined: Fri Nov 04, 2011 8:48 am

Helm me with a function

Post by pri_4all »

I wrote this php code

<?php
echo "<pre>";
$rand=range(1,200);
$arr=array();
print_r($arr);
for($i=0;$i<20;$i++)
{ array_push($arr, array_rand*$rand));
}
print_($arr);
?>

this will print 20 numbers randomly. but now i want to make sure that the random numbers dont repeat themselves. please tell me how to write a function here which will check that the random numbers are not repeating.
please help me
thankyou.

Is this program correct,i wrote a function here to check that the values dont repeat. if its not correct then please help me with the function.

<?php
$rand=range(1,200);
$arr=array();
print_r($arr);
function call()
{
$x=random_nmbers;
if($arr!=$x)
{
array_push;
}

}
for($i=0;$i<20;$i++)
{
array_push($arr, array_rand($rand));
call();
}
echo"<pre>";
sort($arr);
print_r($arr);




?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Helm me with a function

Post by Celauran »

Code: Select all

function randomArray()
{
    $numbers = array();
    while (count($numbers) < 20)
    {
        $x = rand(1, 200);
        if (!in_array($x, $numbers))
        {
            $numbers[] = $x;
        }
    }
    return $numbers;
}
pri_4all
Forum Newbie
Posts: 4
Joined: Fri Nov 04, 2011 8:48 am

Re: Helm me with a function

Post by pri_4all »

thankyou so much :)
Post Reply