westman Posted February 4, 2012 Share Posted February 4, 2012 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 More sharing options...
Don E Posted February 4, 2012 Share Posted February 4, 2012 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 More sharing options...
westman Posted February 4, 2012 Author Share Posted February 4, 2012 ooo i forgot to add,the numbers need to have order with the smalest number at the start and the largest at the end like... 8,34,47,52,53,71 Link to comment Share on other sites More sharing options...
Don E Posted February 4, 2012 Share Posted February 4, 2012 You can use the sort function: http://www.php.net/manual/en/function.sort.php Link to comment Share on other sites More sharing options...
dsonesuk Posted February 4, 2012 Share Posted February 4, 2012 <?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 More sharing options...
westman Posted February 5, 2012 Author Share Posted February 5, 2012 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 More sharing options...
dsonesuk Posted February 5, 2012 Share Posted February 5, 2012 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 More sharing options...
westman Posted February 5, 2012 Author Share Posted February 5, 2012 no, as in the total array stays in 1 $ (verible) at the end like... this verible "$the_random_num" holds an array of "5,23,29,35,68,74" and the verible can be handled in any way after Link to comment Share on other sites More sharing options...
dsonesuk Posted February 5, 2012 Share Posted February 5, 2012 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 More sharing options...
westman Posted February 5, 2012 Author Share Posted February 5, 2012 thank you so much its working now i can use "$the_random_num" in "if" statments thank you again Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.