Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 413 characters in body
Source Link
Gumbo
  • 657.5k
  • 112
  • 792
  • 852

If you want to allow repetitive occurences of characters, you can use this function:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
 $str = '';
 $count = strlen($charset);
 while ($length--) {
 $str .= $charset[mt_rand(0, $count-1)];
 }
 return $str;
}

The basic algorithm is to generate <length> times a random number between 0 and <number of characters> − 1 we use as index to pick a character from our set and concatenate those characters. The 0 and <number of characters> − 1 bounds represent the bounds of the $charset string as the first character is addressed with $charset[0] and the last with $charset[count($charset) - 1].

If you want to allow repetitive occurences of characters, you can use this function:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
 $str = '';
 $count = strlen($charset);
 while ($length--) {
 $str .= $charset[mt_rand(0, $count-1)];
 }
 return $str;
}

If you want to allow repetitive occurences of characters, you can use this function:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
 $str = '';
 $count = strlen($charset);
 while ($length--) {
 $str .= $charset[mt_rand(0, $count-1)];
 }
 return $str;
}

The basic algorithm is to generate <length> times a random number between 0 and <number of characters> − 1 we use as index to pick a character from our set and concatenate those characters. The 0 and <number of characters> − 1 bounds represent the bounds of the $charset string as the first character is addressed with $charset[0] and the last with $charset[count($charset) - 1].

Source Link
Gumbo
  • 657.5k
  • 112
  • 792
  • 852

If you want to allow repetitive occurences of characters, you can use this function:

function randString($length, $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
 $str = '';
 $count = strlen($charset);
 while ($length--) {
 $str .= $charset[mt_rand(0, $count-1)];
 }
 return $str;
}
lang-php

AltStyle によって変換されたページ (->オリジナル) /