Zend - The PHP Company




Arrays

Add Code


Random array  

Type: code fragment
Added by: DamienMcE
Entered: 08/03/2002
Last modified: 03/12/2001
Rating: - (fewer than 3 votes)
Views: 5116
This may save someone 5 minutes. A little function that generates an array of unique random integers.


<?php
function randarray ($max)
{
     
srand((double) microtime() * 1000000);
     
$count =0
     
$currentnum=0;
     
$randomnumbers= array();
     while (
$count<$max)
         {
        
$currentnum=rand (0,$max);    
        
         if (!
in_array($currentnum,$randomnumbers))
        
            {
            
$randomnumbers[$count]=$currentnum;
            
$count++;
            }            
        }                
     return 
$randomnumbers
}
?>


Usage Example




Rate This Script





Search



This Category All Categories