Topic: how can i create random numbers without repetition

hello all

how can i create random numbers without repetition

thanks for help

2 (edited by sibprogsistem 2020-12-31 13:12:29)

Re: how can i create random numbers without repetition

kofa wrote:

hello all

how can i create random numbers without repetition

thanks for help

procedure Form1_Button2_OnClick (Sender: TObject; var Cancel: boolean);
var
i,s,int: integer;
begin
  s:=1;
  while s<>0 do
  begin
    for i := 1 to 5 do
    begin
     int := 1 + Random(100);
    end;
    s:=SQLExecute('SELECT IFNULL(id,0) FROM randomInteger WHERE intege='+IntToStr(int));
  end;
  if (s=0) then begin
       SQLExecute('INSERT INTO randomInteger (intege)VALUES("'+IntToStr(int)+'")');
       UpdateDataBase('randomInteger');
       Form1.TableGrid2.dbUpdate;
  end;
end;
Post's attachments

Attachment icon test.rar 3.82 kb, 236 downloads since 2020-12-31 

3 (edited by kofa 2020-12-31 14:15:54)

Re: how can i create random numbers without repetition

hello sibprogsistem

thank you very much

i tired to  apply this code to my file

can you please solve my file

thanks for help

Post's attachments

Attachment icon random.rar 293.62 kb, 244 downloads since 2020-12-31 

4 (edited by sibprogsistem 2020-12-31 18:22:25)

Re: how can i create random numbers without repetition

kofa wrote:

hello sibprogsistem

thank you very much

i tired to  apply this code to my file

can you please solve my file

thanks for help

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
i,s,r: Integer;
str:Boolean;
begin
  for i:=0 to Form1.ComponentCount-1 do begin
      if (Form1.Components[i] is TdbEdit) then begin
          repeat
          str:=False;
          r:=random(100);
              for s:=0 to Form1.ComponentCount-1 do begin
                 if (Form1.Components[s] is TdbEdit) then
                         if (TdbEdit(Form1.Components[s]).Value=r) then str:=True;
               end;
           if (str=False) then TdbEdit(Form1.Components[i]).Value:=r;
          until str=False;
      end;
   end;
end;

Re: how can i create random numbers without repetition

hello derek

thank you very much

but how can i use for loop because i have more text boxes

thanks for help

Re: how can i create random numbers without repetition

hello sibprogsistem

thank you very much

when i press the button first time i found empty textboxes

can you check it again

thanks for help

7 (edited by sibprogsistem 2020-12-31 18:50:31)

Re: how can i create random numbers without repetition

kofa wrote:

hello sibprogsistem

thank you very much

when i press the button first time i found empty textboxes

can you check it again

thanks for help

я поменял
until str=True;
на
until str=False;
и все поля заполняются

Post's attachments

Attachment icon random.rar 4 kb, 268 downloads since 2020-12-31 

Re: how can i create random numbers without repetition

hello sibprogsistem

thank you very much

works fine

happy new year

Re: how can i create random numbers without repetition

hello sibprogsistem

How can I exclude TextBox1

this code not working


procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
i,s,r: Integer;
str:Boolean;
begin
  for i:=2 to Form1.ComponentCount-1 do begin
      if (Form1.Components[i] is TdbEdit) then begin
          repeat
          str:=False;
          r:=random(100);
              for s:=2 to Form1.ComponentCount-1 do begin
                 if (Form1.Components[s] is TdbEdit) then
                         if (TdbEdit(Form1.Components[s]).Value=r) then str:=True;
               end;
           if (str=False) then TdbEdit(Form1.Components[i]).Value:=r;
          until str=False;
      end;
   end;
end;

thanks for help

Re: how can i create random numbers without repetition

kofa wrote:

hello sibprogsistem

How can I exclude TextBox1

в  этом примере я исключил edit2

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
i,s,r: Integer;
str:Boolean;
begin
  for i:=0 to Form1.ComponentCount-1 do begin
      if (Form1.Components[i] is TdbEdit) AND ((TdbEdit(Form1.Components[i]).Name<>'Edit2')) then begin
          repeat
          str:=False;
          r:=random(100);
              for s:=0 to Form1.ComponentCount-1 do begin
                 if (Form1.Components[s] is TdbEdit) then
                         if (TdbEdit(Form1.Components[s]).Value=r) then str:=True;
               end;
           if (str=False) then TdbEdit(Form1.Components[i]).Value:=r;
          until str=False;
      end;
   end;
end;

Re: how can i create random numbers without repetition

thank you sibprogsistem

You are the king of codes

happy time and happy new year