-3

How to create 7 or Nth digits alphanumeric coupon code in a fast way? I have checked to use array_rand or rand or mt_rand which function should I prefer. To make my code fast and get more random number.

Requirement:

  1. It should be take random number from 0-9 and A-Z (not lowercase a-z).

  2. Starting first to three characters should be from A-Z and remaining should be 0-9.

  3. 0-9 and A-Z can be repeat.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nits
  • 520
  • 6
  • 21
  • 1
    Possible duplicate of [Generating (pseudo)random alpha-numeric strings](http://stackoverflow.com/questions/48124/generating-pseudorandom-alpha-numeric-strings) – Asur May 11 '16 at 11:12
  • How fast exactly you need it? Generating random numbers - pseudo or real isn't fast in anyway. How often you need to generate coupons and how many of them? – Pavel Petrov May 11 '16 at 11:16
  • You could just get the first 7 characters returned by [`uniqid`](http://php.net/manual/en/function.uniqid.php) and uppercase them. – apokryfos May 11 '16 at 11:30

6 Answers6

2

Requirement:
1) It should be take random number from 0-9 and A-Z ( not lowercase a-z).
2) Starting first to three characters should be from A-Z and remaining should be 0-9.
3) 0-9 and A-Z can be repeat.

You can use substr() and str_shuffle, i.e.:

$partOne =  substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 3);    
$partTwo =  substr(str_shuffle("0123456789"), 0, 4);  
echo $partOne.$partTwo;
//ZPN1690

Ideone Demo:

http://ideone.com/OU3DuM

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • $alphabet=array('A','B','C'); $partOne=null; for($i=0;$i<3;$i++){ $partOne .= $alphabet[mt_rand(0, 2)]; } same for $partTwo also I have to do. To generate 3) 0-9 and A-Z can be repeat. – Nits May 11 '16 at 11:50
1

Using random_bytes or openssl_random_pseudo_bytes.
Pass a length of 4 bytes as a parameter, remove the last character to get the 7 string coupon code.

for ($i=0;$i<=1000;$i++){

    $bytes = openssl_random_pseudo_bytes(4);
    $hex   = bin2hex($bytes);

    $coupon = substr($hex, 0, -1);
    print $coupon . PHP_EOL;

}

Will output

...
157d224
a46b8a8
83e208b
16a338a
ad7318a
55e405a
63e786a
ca016c5
...
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
1

Just make it easy, Use str_suffle and substr for random string.

$length = 7;    
$randomString =  substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);    
echo $randomString;
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

The below function will create a random string

function generateRandomString($length = 7) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

echo genereteRandomString();

You can pass the desired length to the function too. The character for the string can be limited in the $characters variable

Arun
  • 3,640
  • 7
  • 44
  • 87
0
function generateToken($length = 7)
{
    $chars = 'abcdefghijklmnopqrstuvwxyz1234567890'; // add characters here
    $token = '';
    while(strlen($token) < $length) {
        $token .= $chars[mt_rand(0, strlen($chars)-1)];
    }
    return $token;
}
Dale
  • 10,384
  • 21
  • 34
0

It will create n digit randon number. Try this:

$n = 7;
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for ($i = 0; $i < $n; $i++) {
    $res .= $chars[mt_rand(0, strlen($chars)-1)];
}
Jayesh Chitroda
  • 4,987
  • 13
  • 18