user1661548
user1661548

Reputation:

Generate Serial Numbers with an algorithm in PHP

I just started working on a project and before I begin I have a few question about a peice of code I need to put together, and how it will need to work.

This code will be generating serial numbers that when ran through an algorithm will equal an identifier. The algorithm is nothing special.


The serial number will need to be broken up in to 4 sets of 5: (Preferably hexadecimal)
Example: 00A00-0B000-F00C0-0000D

The identifier will just be 1 set of numbers:
Example: 15456548

What I am looking to create is a script that could add(array_sum?) the hexadecimals in a serial number together, dived by 10 and equal the identifier(15456548). The script will need to create as many instances of the serial numbers as I tell it.


Program Example:

Identifier: 22807.4
How Many Serial Numbers To Make: 2

Serial Numbers:
45154-54587-58458-69875
55457-45584-18658-49578

(These serial numbers equal the identifier(22807.4) when added up and divided by 10)

I'm not sure if anyone could help me come up with something, or at least explain the process, but I've never built anything like this in PHP before.

Upvotes: 1

Views: 4578

Answers (2)

Amit
Amit

Reputation: 1885

Another sample. You need to add few things to it. Hopefully you will realize them when you run the program.

<?php 
    $num = '1234';
    $serNums = findSerialNum(1234, 5);
    echo var_export($serNums, true);

    function findSerialNumSingle($num)
    {
        $serialNum = array();
        $target = $num * 10;
        // select some heuristic. We have to generate 5 sets. How shall we 
        // distribute the numbers between the 5 sets? 
        // Obviously one set can exceed the num * 10 itself.
        // Lets select that as the heuristic, that the first number can be max that. 
        $remaining = $target;
        $soFar = 0;
        for($i = 0; $i < 5; $i++)
        {
            // select the entry that we would fill. Randomize it. 
            while(true)
            {
                $idx = rand(0, 4);
                if(!isset($serialNum[$idx])) // is the entry already filled in?
                    break;
            }

            $t = rand(0, $target - $soFar); // find number between 0 and remain number
            $serialNum[$idx] = $t;
            $soFar += $t;
        }

        // put serial number is proper format
        $s = '';
        for($i = 0; $i < 5; $i++)
        {
            $s .= sprintf('%05X', $serialNum[$i]) . '-';
        }
        return $s;
    }
    function findSerialNum($num, $numInstances)
    {
        $serNums = array();
        for($i = 0; $i < $numInstances; $i++)
        {
            while(true)
            {
                // loop till you find a distinct serial number
                $t = findSerialNumSingle($num);
                if(array_search($t, $serNums) == FALSE)
                {
                    $serNums[$i] = $t;
                    break;
                }
            }
        }
        return $serNums;
    }
?>

Upvotes: 2

Jason Satterfield
Jason Satterfield

Reputation: 147

The following code will do what you're looking for, but does nothing in the way of type-checking and ensuring that parameters match the proper lengths. For example, you'll want to make sure that the string $serial parameter is actually in the format of 'XXXXX-XXXXX-XXXXX-XXXXX'. You'll also want to make sure that the array $serial parameter is composed of 4 integers.

function TextSerialToIdentifier(string $serial) {
    $a = str_split($serial, 5);
    $b = array();
    foreach($a as $v) {
        array_push($b, intval($v, 16));
    }
    return ArraySerialToIdentifier($b);
}

function ArraySerialToIdentifier(array $serial) {
    $id = 0;
    foreach($serial as $i) {
        $id += $i;
    }
    $id /= 10;
    return intval($id);
}

function CreateSerial($id) {
    $serial = '';
    $id *= 10;
    for($i = 0; $i < 5; $i++ ) {
        $val = rand(0x00000, 0xFFFFF);
        $id -= $val;
        $serial = $serial . '-' . $val;
    }
    return substr($serial, 1);
}

Upvotes: 0

Related Questions