Miscellaneous Code

  Home arrow Miscellaneous Code arrow Multiple Unique Random Numbers
MISCELLANEOUS CODE

Multiple Unique Random Numbers
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2004-06-24

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap