Jump to content

random number


westman

Recommended Posts

i have a scripte working well to get 6 random numbers in an array ;)

$random_num1 = rand(1, 75);$random_num2 = rand(1, 75);$random_num3 = rand(1, 75);$random_num4 = rand(1, 75);$random_num5 = rand(1, 75);$random_num6 = rand(1, 75);$the_random_num = '' . $random_num1 . ',' . $random_num2 . ',' . $random_num3 . ',' . $random_num4 . ',' . $random_num5 . ',' . $random_num6 . '';

this will give me a value of3,27,7,43,43,3or2,42,17,78,78,22and so on... but how do i get 6 numbers that is never the same?

Link to comment
Share on other sites

You can use the array_unique function but that will get rid of of the duplicate and it will not return 6 values if that's what you want; meaning 6 values returned at all times. You can see if it returns less than 6(you can use the count function for this: http://www.php.net/manual/en/function.count.php), and if it does, you can add a random number to the end of the array making it 6 again. Give it another check, if it returns 6, then you have no duplicates and they're all random. I'm sure there are other ways some of the others will demonstrate for ya. :)

Link to comment
Share on other sites

<?php$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num )    {    $current_rand_num = mt_rand(1,75);    if (!in_array($current_rand_num,$random_numbers))        {        $random_numbers[] = $current_rand_num;        echo $current_rand_num.', ';        }    }echo ' <br />';sort($random_numbers);foreach($random_numbers as $random_num)    {     echo $random_num.", ";    }?>

Link to comment
Share on other sites

thank you very much and it works grate i tryd to stop the echo and save them in $ but it did not work here is my code...

<?php$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num )    {    $current_rand_num = mt_rand(1,75);    if (!in_array($current_rand_num,$random_numbers))	    {	    $random_numbers[] = $current_rand_num;$current_rand_num .= ', ';	    }    }sort($random_numbers);foreach($random_numbers as $random_num)    {$random_num .= ", ";    }$the_random_num = $random_num;?>

how do i save them in $

Link to comment
Share on other sites

I've been through this before? haven't I seems very familiar, oh well never mind.

<?php$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num )    {    $current_rand_num = mt_rand(1,75);    if (!in_array($current_rand_num,$random_numbers))        {        $random_numbers[] = $current_rand_num;        //echo $current_rand_num.', ';        }    }//echo ' <br />';sort($random_numbers);$count=1;foreach($random_numbers as $random_num)    {    ${'random_number'.$count} = $random_num;    $count++;    }    echo $random_number1.', '.$random_number2.', '.$random_number3.', '.$random_number4.', '.$random_number5.', '.$random_number6;?>

Link to comment
Share on other sites

That is not a array, that is a text string of variables brought togetheryou can achieve the same result with<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script><style type="text/css"></style></head><body><?php$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num ){$current_rand_num = mt_rand(1,75);if (!in_array($current_rand_num,$random_numbers)){$random_numbers[] = $current_rand_num;}}sort($random_numbers);$the_random_num="";for($i=0;$i<count($random_numbers);$i++){//$the_random_num.=$random_num.', ';$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i];}echo $the_random_num;?></body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...