samedi 9 septembre 2017

php random number function returning same value each time is called

I am trying to genarate random number of any length i want to make product id and order id. But the problem here is when i create new item giving the length of the id 11 it will genarte the same id use before it doesn't change the numbers.

Here is my php code

<?php
function EventRang($length = 10, $type){
    switch($type){
        case 'int':
        //$keyspace = mt_rand(10000000000, 99999999999).date("Ymd").rand();
        $keyspace = str_pad(rand(0, pow(10, $length)-1), $length, '0', STR_PAD_LEFT);
        break;
        case 'char':
        $keyspace = date('Fl').'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        break;
        case 'str':
        $keyspace = time().'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        break;
        case 'oid':
        //$keyspace = date("Ymd").time().rand().mt_rand(10000000000, 99999999999);
        $keyspace = date("Ymd").time().str_pad(rand(0, pow(10, $length)-1), $length, '0', STR_PAD_LEFT);
        break;
        default:
        $keyspace = time().'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        break;
    }
    $charactersLength = strlen($keyspace);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $keyspace[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
?>

this is always the output 2147483647




Aucun commentaire:

Enregistrer un commentaire