1

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

11
  • 1
    pow(10,11) exceeds 32-bit numbers Commented Sep 9, 2017 at 12:16
  • 5
    2147483647 is a magic number that you should learn to recognise; it is the max signed int value for 32-bit PHP Commented Sep 9, 2017 at 12:18
  • @MarkBaker please help fix it, am new to php that all i can do for now i don't have idea about that magic number Commented Sep 9, 2017 at 12:20
  • What characters should be returned for an oid value? You seem to be defining a "mask" rather than a "keyspace", ie a list of valid characters.... possibly better to set the keyspace simply to the list of valid caharacters, and then do any special formatting you might want after generating the random value Commented Sep 9, 2017 at 12:22
  • @MarkBaker for oid i want to use the current date and time then add random number at the end date("Ymd").time()4488765. Please just help me fix it the right way it should be i will have time to look at it and learn more Commented Sep 9, 2017 at 12:23

1 Answer 1

1

Because sometimes answers need to be explicit

function EventRang($length = 10, $type = 'int') {
    // Define the set of characters permitted for the random element for each type
    switch($type) {
        case 'int':
        case 'oid':
            $keyspace = '0123456789';
            break;
        case 'char':
            $keyspace = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 'str':
        default:
            $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    }

    // Generate the random characters/digits
    $charactersLength = strlen($keyspace);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $keyspace[rand(0, $charactersLength - 1)];
    }

    // apply the masking
    switch($type) {
        case 'int':
            return $randomString;
        case 'char':
            return date('Fl').$randomString;
            break;
        case 'str':
            return time().$randomString;
            break;
        case 'oid':
            return date("Ymd").time().$randomString;
            break;
        default:
            return time().$randomString;
    }
}

You need to separate the masking from the keyspace; keyspace is simply the set of characters to use for the random element of the output; if you try to apply the masking as part of the keyspace, you're simply skewing the randomness, and not applying the masking either.

Also note that the function definition needs a default value for type as well, because it comes after length in the argument list, and length has a default value

Sign up to request clarification or add additional context in comments.

3 Comments

The int, is working very fine now but other once return more than the given value. I think i know why because of the making let me try fixing it
If the value is too long, then substring it before returning.... I didn't know whether you wanted 11 random digits in addition to the masking, or including the masking
Though I will say that the date masking for char is just weird; and a value of 11 for oid isn't going to give much variation because date("Ymd").time() is already longer than 11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.