Search code examples
phpunique

How to generate unique small integer from a larger integer


I want to generate a unique small integer between 1 and 3 from a larger one coming from an id. E.g:

id: 348593 ----> generated: 2

id: 98762  ----> generated: 3 

id: 12     ----> generated: 1 

I did it using module for two numbers (1 and 2):

$id = 43784643;
$module = $id % 2; // 0 if even, 1 if odd

$generated = $module + 1;

How would you do this, considering that the small number must be always the same for the same id?


Solution

  • You seem to already have a good solution: modulus.

    $generated = ($id % 3) + 1