This question is an exact duplicate of:
I need to generate unique random numbers (bigint) in Postgresql with a fixed max length of 13 digits. I've found some threads where was used a sequence encrypted using the 64 bit pseudo_encrypt function, but the returned number was not with a fixed max length, in the 32 bit case the max length is 10 digits (int), and for the 64 bit is 19 (bigint).
What i need is: get an encrypted random sequence with a fixed max length of 13 digits, where the min value is 1 and the max value is 9999999999999.
Is it possible to modify the 64 bit pseudo_ecrypt function in order to get this result? Or if is not possible, there are other methods to get an unique sequence with this requirements?
Pseudo Encrypt function (64bit)
CREATE OR REPLACE FUNCTION pseudo_encrypt(VALUE bigint) returns bigint AS $$
DECLARE
l1 bigint;
l2 bigint;
r1 bigint;
r2 bigint;
i int:=0;
BEGIN
l1:= (VALUE >> 32) & 4294967295::bigint;
r1:= VALUE & 4294967295;
WHILE i < 3 LOOP
l2 := r1;
r2 := l1 # ((((1366.0 * r1 + 150889) % 714025) / 714025.0) * 32767*32767)::int;
l1 := l2;
r1 := r2;
i := i + 1;
END LOOP;
RETURN ((l1::bigint << 32) + r1);
END;
$$ LANGUAGE plpgsql strict immutable;
Aucun commentaire:
Enregistrer un commentaire