943,657 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1963
  • PHP RSS
Aug 8th, 2009
0

PHP random number check

Expand Post »
Hai,

I had a small doubt in my php script.
I need to select 10 random numbers from a range ( for eg: 2-88) and stored it in an array.
For eg:
$number=rand(2,88); This returns only one value from the range 2-88. I need 15 random numbers from the given range and also I need 15 different numbers and finally stored it in an array. ie $number[1],$number[2] .......

Please help me .... Thanks in Advance
Rajeesh
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
rajeesh_rsn is offline Offline
265 posts
since Sep 2008
Aug 8th, 2009
0

Re: PHP random number check

1. Keep an array to hold the random numbers

2. In order to add an element to this array, generate a random number in the required range, and check the array to see if the generated number is already a part of the array (using foreach)

3. If the generated random number is not a part of the array, append it to the array, else goto step 2.

4. Finish the algorithm when the array holds 15 elements

----

Alternatively, you can use 'in_array()' to check if a value exists in an array

http://www.w3schools.com/PHP/func_array_in_array.asp
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Aug 8th, 2009
-1

Re: PHP random number check

This code will help you in retrieving 15 random numbers and storing them in an array,.....

php Syntax (Toggle Plain Text)
  1. <?php
  2. $j = 0;
  3. $data = array();
  4. while($j < 15)
  5. {
  6. $value = rand(2,88);
  7. if(!(in_array($value,$data)))
  8. { $j = $j + 1;
  9. $data[$j] = $value; }
  10. }
  11. foreach($data as $keys)
  12. { echo $keys."<br>"; }
  13. ?>

You use this code to get 15 random numbers and you code to select 10 nums as you wish,..
Last edited by peter_budo; Aug 9th, 2009 at 8:23 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rajesh_kanna is offline Offline
4 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Unable To Upload File to Ftp
Next Thread in PHP Forum Timeline: How do I cirumvent case sensitivity issues in the admin panel of phpBB 2.0?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC