You cannot guarantee no collisions since one of your input spaces is the 9 digit number and your output space is the same size. You can probably code to reduce the possibility. If you could use another base like 64 then your output space is larger than your input space.

Specifically your input space is basically a 30 bit quantity + a 32 bit quantity which you want to store uniquely in a 30 bit space. For each value in the output, there are 2**32 inputs which will give you that output.

If you could use base64 then you could get a 62 bit quantity SID.IP into 9 character places.

If you used for example Digest::MD5 you can get an almost certainly unique quantity base16 in 32 bytes or base64 in 22 bytes. This uses an existing module implementing a strong digest algorithm which is the math problem you are wanting to solve.

In summary you can make up a program to map the ip address into the existing session id and you can work to avoid collisions but ultimately the space is too small to never have a collision

If you can throw the unique ids away after a time then by using time generated as a small factor you could probably get a system which would not be likely to collide for the lifetime of the session id. This is the only solution that seems to not have a collision hazard but the larger the time interval the more likely a collision.


In reply to Re: integer encoder/decoder by dga
in thread integer encoder/decoder by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.