Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,I have generated random strings,but now i want multiple number of random strings at a time.. what can i do in my following coding??

PHP
function rand_string($l){  
  $s= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  srand((double)microtime()*1000000);  
  
  {
  for($i=0; $i<$l; $i++)
  {  
      $rand.= $s[rand()%strlen($s)];                       
      
  }
  
  return $rand; 
 } 
 $l=8;  
 $string=rand_string($l);
 print $string;

I know I have to use here FOR loop with rand function...but how i don't have any idea please help;;
Posted
Comments
Rohit Shrivastava 22-Oct-12 14:22pm    
You could probably pass the seed to rand function, though i am not familiar with PHP but random function normally takes seed.

1 solution

function rand_string($l){  
  $s= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  srand((double)microtime()*1000000);  
  
  {
  for($i=0; $i<$l; $i++)
  {  
      $rand.= $s[rand()%strlen($s)];                       
      
  }
  
  return $rand; 
 } 
 $l=8;

for($i=0;$i<10;$i++)
  $random_strings[] = rand_string($l);

//$random_strings is an array containing all the random strings generated.

print_r($random_strings);
 
Share this answer
 
v3
Comments
project virus 23-Oct-12 6:10am    
thanks ankit.but can u plz tell me how to print it in array?
Ankit Jain 23-Oct-12 13:35pm    
updated the ans.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900