Let me know,how can i pick randomly six numbers in between 6 to 49

output like this :23 44 3 11 21
in php only
etc..........

Recommended Answers

All 3 Replies

PHP already has a built in function that randomizes a number given the minimum and the maximum.

$random = rand(6, 49);

Here's the link to the manual that describes the function in case you encounter problems http://www.php.net/manual/en/function.rand.php

ya , but i need six numbers from 6 to 49 .
i.e 4 33 21 16 12 44 like this

also i need to print 3 or 4 rows like above format
means
4 33 21 16 12 44
22 12 8 39 41 2
5 29 34 11 2 44

etc.......

PHP already has a built in function that randomizes a number given the minimum and the maximum.

$random = rand(6, 49);

Here's the link to the manual that describes the function in case you encounter problems http://www.php.net/manual/en/function.rand.php

Well, if you had explained it earlier, I would have replied differently. The answer you're looking for is easy, you can use this piece of code:

$num_rows = 3; // number of rows u want to generate 
for($cur_row = 1; $cur <= $num_rows; $cur_row++)
{
   $random = array(); // array of random values
   while(count($random) < 6) // check whether you have the number of values needed
   {
      $value = rand(6, 49);
      // I assume you need unique values
      if(array_search($value, $random) === FALSE) // value is not yet in array
      {
         // add value to array
         $random[] = $value;
      }
   }
 
   foreach($random as $index => $value)
   {
      print $value;
      if ($index < (count($random) - 1)) // check if its not the last element
         print " "; // print a space
      else print "\n"; // last element, new line
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.