| | |
PHP random number check
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2008
Posts: 138
Reputation:
Solved Threads: 0
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:
Please help me .... Thanks in Advance
Rajeesh
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
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
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
My blog on .NET- http://dotnet.tekyt.info
•
•
Join Date: Aug 2009
Posts: 4
Reputation:
Solved Threads: 0
This code will help you in retrieving 15 random numbers and storing them in an array,.....
You use this code to get 15 random numbers and you code to select 10 nums as you wish,..
php Syntax (Toggle Plain Text)
<?php $j = 0; $data = array(); while($j < 15) { $value = rand(2,88); if(!(in_array($value,$data))) { $j = $j + 1; $data[$j] = $value; } } foreach($data as $keys) { echo $keys."<br>"; } ?>
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.
![]() |
Similar Threads
- Random number generation from an array without repeats (C)
- Random number sequences (C++)
- Random number Sequence Generation (C++)
- adding random number (PHP)
- Random number generation (Java)
- How to generate a double random number by using PHP (PHP)
- how to count integer from random number (C)
- random number (C++)
- random number generator (C++)
Other Threads in the PHP Forum
- Previous Thread: Unable To Upload File to Ftp
- Next Thread: How do I cirumvent case sensitivity issues in the admin panel of phpBB 2.0?
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





