Page 1 of 1

Helm me with a function

Posted: Fri Nov 04, 2011 8:52 am
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);




?>

Re: Helm me with a function

Posted: Fri Nov 04, 2011 9:03 am
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;
}

Re: Helm me with a function

Posted: Fri Nov 04, 2011 9:13 am
by pri_4all
thankyou so much :)