This function returns an array of unique random numbers. It works just like PHP's rand() with an extra argument for the count of the returned array.
**Update: Added error checking and an optional 4th argument to set the length of the returned numbers.
By : tr4nc3
<?php
# Multiple Unique Random Numbers
#
# array mrand ( int min, int max, int count [, int strlen ] )
function mrand($l,$h,$t,$len=false){
if($l>$h){$a=$l;$b=$h;$h=$a;$l=$b;}
if( (($h-$l)+1)<$t || $t<=0 )return false;
$n = array();
if($len>0){
if(strlen($h)<$len && strlen($l)<$len)return false;
if(strlen($h-1)<$len && strlen($l-1)<$len && $t>1)return false;
do{
$x = rand($l,$h);
if(!in_array($x,$n) && strlen($x) == $len)$n[] = $x;
}while(count($n)<$t);
}else{
do{
$x = rand($l,$h);
if(!in_array($x,$n))$n[] = $x;
}while(count($n)<$t);
}
return $n;
}
## Samples ##
if(!$numbers = mrand(1000,9999,10,4))die("error");
// 20 random, all 7 chars in length
# $numbers = mrand(0,9999999,20,7);
// 20 random, length doesn't matter
# $numbers = mrand(0,9999999,20);
# $numbers = mrand(8000,10000,1,5) // ok
# $numbers = mrand(8000,10000,2,5) // error
# $numbers = mrand(120,100,20); // ok
# $numbers = mrand(100,120,21); // ok
# $numbers = mrand(120,100,21); // ok
# $numbers = mrand(120,100,22); // error
# $numbers = mrand(100,120,22); // error
# $numbers = mrand(120,120,1); // ok
# $numbers = mrand(120,120,2); // error
# $numbers = mrand(120,120,0); // error
# $numbers = mrand(1000000,9999999,3000);
// Sort the array
# sort($numbers);
// Display the array
echo '<pre>';
print_r($number);
echo '</pre>';
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |