Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 13, 2021 12:43
Show Gist options
  • Select an option

  • Save simonthompson99/3218136afda7a628248eec8ed2f31e14 to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/3218136afda7a628248eec8ed2f31e14 to your computer and use it in GitHub Desktop.

Revisions

  1. simonthompson99 revised this gist Jan 13, 2021. No changes.
  2. simonthompson99 created this gist Jun 30, 2020.
    28 changes: 28 additions & 0 deletions pseudo-random-integer.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    create sequence testes.test_id_seq
    ;

    create or replace function pseudo_encrypt(value int) returns int as $$
    declare
    l1 int;
    l2 int;
    r1 int;
    r2 int;
    i int:=0;
    begin
    l1:= (value >> 16) & 65535;
    r1:= value & 65535;
    while i < 3 loop
    l2 := r1;
    r2 := l1 # ((((1366 * r1 + 150889) % 714025) / 714025.0) * 32767)::int;
    l1 := l2;
    r1 := r2;
    i := i + 1;
    end loop;
    return ((r1 << 16) + l1);
    end;
    $$ language plpgsql strict immutable;

    create table testes.id_test (
    test_uid uuid not null default uuid_generate_v4(),
    id varchar not null default concat('sp', lpad(pseudo_encrypt(nextval('testes.test_id_seq')::int)::text, 10, '0'))
    );