Topic: Random and unique number / sequence generation.

Is there a a way to generate a unique and random or sequenced number which would be saved along each new entry in a column? I would like to automatically fill out a field to use as a QrCode/Barcode reference. I know Sqlite does not have a sequence function.

Re: Random and unique number / sequence generation.

tcoton wrote:

Is there a a way to generate a unique and random or sequenced number which would be saved along each new entry in a column? I would like to automatically fill out a field to use as a QrCode/Barcode reference. I know Sqlite does not have a sequence function.

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
result:boolean = False;
i:integer;
number: string;
begin
  for i:=0 to StrToInt(Form1.Edit1.text)-1 do
  begin
    while result = False do
    begin
      number := IntToStr(RandomRange(1,10000000));
      result := posnumber(number);
    end;
    if result = true then Form1.Memo1.Lines.Add(number);
    result := False
  end
end;

function posnumber (number:string;): boolean ;
var
i,n:integer;
begin
  if (Form1.Memo1.Lines.Count = 0) then result := True else
  begin
    for i:=0 to Form1.Memo1.Lines.Count-1 do
    begin
      if ( Pos(Form1.Memo1.Lines[i],number)) then n:=1;
    end;
    if (n=0) then result := True;
    n:=0;
  end;
end;
Post's attachments

Attachment icon test.rar 3.51 kb, 142 downloads since 2023-08-01 

Re: Random and unique number / sequence generation.

Thanks, that does the job but I need to fill just one text box at a time, I am struggling to adapt this to a textbox instead of a memo.

Re: Random and unique number / sequence generation.

Hi TCoton, Vladimir,
Can you not use a counter (for example, set it to start at 1000000) as your qr code / bar code reference?
Also, if you are still having problems with leading or trailing zeroes when using qr codes in FastReport, you could try creating a calculated field (defined as 'text') and setting it to equal the counter.  Then use the calculated field in your report instead.
Derek.

Re: Random and unique number / sequence generation.

Hi Derek,

I had the same idea after searching on the forum and I have seen a counter example that could do it from Dmitry where the counter and the formatted id are concatenated.

Re: Random and unique number / sequence generation.

UUID?

https://habr.com/ru/companies/vk/articles/522094/

Визуальное программирование: блог и телеграм-канал.

Re: Random and unique number / sequence generation.

function GUID:string;
var s:variant;
begin
   s := CreateOleObject('Scriptlet.TypeLib');
   result := Copy(s.Guid, 2, 36);
   s := 0;
end;

8 (edited by sparrow 2023-08-03 16:45:45)

Re: Random and unique number / sequence generation.

https://www.sqlite.org/draft/lang_coref … randomblob

select lower(hex(randomblob(16)))

Re: Random and unique number / sequence generation.

@k425, vovka3003 and sparrow,

A big thank you, UUID was my initial idea but I have no idea how to implement it with MVDB since you need to use sha or md5 is there a hash feature in MVDB?

I will try the 2 other solutions whichever is simpler and more capable of uniqueness of the generated UIDs.

Re: Random and unique number / sequence generation.

Something like this

Post's attachments

Attachment icon guid.zip 326.07 kb, 178 downloads since 2023-08-03 

Re: Random and unique number / sequence generation.

Looks neat and easy to implement, thanks Sparrow!