The random function ... lazarus / 9-Editor ---------------------------------------------------------------------- GoDxNero · 2009-10-17 11:06 Its about 2 dice's. a is the eyes of dice1 and b is the eyes of dice2. rt1 is the roltime of dice1 and its random. So if roltime1 = 0 then a will stop with randomize, same goes for b. cd = countdown . However I always got the same result each time I close the game and restart it. What is wrong ? procedure TForm1.PlayClick(Sender: TObject); begin   roll := 0;   cd := 0;   rt1 := 2 + random(5);   rt2 := 2 + random(5);   r1 := rt1;   r2 := rt2;   Label3.Caption := IntToStr(r1);   Label4.Caption := IntToStr(r2);   Timer1.Enabled := True; end; procedure TForm1.Timer1Timer(Sender: TObject); begin   roll := roll + 0.0157;   if roll < rt1   then a := 1 + random(6)   else a := a;   if roll < rt2   then b := 1 + random(6)   else b := b;   Label1.Caption := IntToStr(a);   Label2.Caption := IntToStr(b);   cd := cd + 0.0157;   if cd > 1 then   begin     cd := 0;     if r1 > 0 then     r1 := r1 - 1;     if r2 > 0 then     r2 := r2 - 1;   end;   Label4.Caption := IntToStr(r1);   Label4.Caption := IntToStr(r2);   if (r1 = 0) and (r2 = 0) then   Timer1.Enabled := False; end; ---------------------------------------------------------------------- Martin_fr · 2009-10-17 11:32 do you call randomize ? You should call it once when your app starts. ---------------------------------------------------------------------- GoDxNero · 2009-10-17 12:08 Oh , didnt know about that   . I just read http://www.delphibasics.co.uk/RTL.asp?Name=Randomize about randomize But I still dont get at all how it works . Do I have to always use the ' i ' and the ' loop ' or if I just put 'randomize' before any random function then it will works ? Like this ? procedure TForm1.PlayClick(Sender: TObject); begin  randomize;   roll := 0;   cd := 0;   rt1 := 2 + random(5);   rt2 := 2 + random(5);   r1 := rt1;   r2 := rt2;   Label3.Caption := IntToStr(r1);   Label4.Caption := IntToStr(r2);   Timer1.Enabled := True; end; will it work in the Timer too ? procedure TForm1.Timer1Timer(Sender: TObject); begin   randomize;   roll := roll + 0.0157;   if roll < rt1   then a := 1 + random(6)   else a := a;   if roll < rt2   then b := 1 + random(6)   else b := b;   Label1.Caption := IntToStr(a);   Label2.Caption := IntToStr(b);   cd := cd + 0.0157;   if cd > 1 then   begin     cd := 0;     if r1 > 0 then     r1 := r1 - 1;     if r2 > 0 then     r2 := r2 - 1;   end;   Label4.Caption := IntToStr(r1);   Label4.Caption := IntToStr(r2);   if (r1 = 0) and (r2 = 0) then   Timer1.Enabled := False; end; ---------------------------------------------------------------------- Leledumbo · 2009-10-19 05:52 You just need to call it once (at OnCreate for example), and it will hold throughout the program. ---------------------------------------------------------------------- GoDxNero · 2009-10-23 21:09 ok . btw I have one more question . If I want to random 3 numbers ( a, b, c ) . a := 1 + random(6); b := 1 + random(6); c := 1 + random(6); But I dont want them to be the same. So if a = 3 then b can be any number from 1~6 except 3 . and if b = 4 then c can be any number from 1~6 except 3 and 4. Is there a simple way to write such code with even more variable ? ---------------------------------------------------------------------- eny · 2009-10-23 21:17 Create an integer list and put all valid numbers in the list (in your case 1..6). Generate a random number that is the index in the list (0..5 if the list indexes are zero-based). Extract the number at that position and generate the next index number. Which of course will be an index number from 0..4. Again extract the chosen number from the list and generate the next index number (0..3). etc. etc. ---------------------------------------------------------------------- GoDxNero · 2009-10-23 21:20 could you please give me a piek at this code ? I dont understand about the index part, how to write it... srry for being noob ---------------------------------------------------------------------- theo · 2009-10-23 21:58 One possible way: Code: [Select]function RandomSort(Item1, item2: Pointer): integer; begin  Result := -1 + Random(3); end; procedure TForm1.Button1Click(Sender: TObject); const LowNumber=1; const HighNumber=6; const CountNumbers=3; var L:TList; i, nextnum:integer; begin   Memo1.Lines.Clear;   L:=TList.create;   for i:=LowNumber to HighNumber do L.Add(Pointer(i));   Randomize;   L.Sort(@RandomSort);   for i:=0 to CountNumbers-1 do   begin     nextnum:=PtrInt(L[i]);     Memo1.Lines.Add(IntToStr(nextnum));   end;   L.free; end; ---------------------------------------------------------------------- Martin_fr · 2009-10-23 22:19 Quote from: GoDxNero on October 23, 2009, 11:09:06 pmIf I want to random 3 numbers ( a, b, c ) . a := 1 + random(6); b := 1 + random(6); c := 1 + random(6); But I dont want them to be the same. So if a = 3 then b can be any number from 1~6 except 3 . a := 1 + random(6); b := 1 + random(5); if b >= a then inc(b); c := 1 + random(4); if c >= min(a,b) then inc(c); if c >= max(a,b) then inc(c); min and max are from the "Math" unit If you need more than 3 diff numbers it will be more complex, and you better go with theo's code ---------------------------------------------------------------------- GoDxNero · 2009-10-23 22:21 I will try to bash my brain out with Theo code xD ---------------------------------------------------------------------- davesimplewear · 2009-10-24 08:00 Library RxNew contains rxDice, why not checkout their code, why reinvent the wheel? ---------------------------------------------------------------------- brunello · 2009-10-24 08:39 One possible way: procedure TForm1.Button1Click(Sender: TObject); const LowNumber=1; const HighNumber=6; const CountNumbers=3; var   i, n: integer;   s: string;   lista: TStringList; begin   lista := TStringList.Create;   for i := LowNumber to HighNumber do     lista.Add(IntToStr(i));   s := '';   randomize();   for i := 0 to (CountNumbers - 1) do     begin       n := random(HighNumber - i);       s := s + ' ' + lista[n];       lista.Delete(n);     end;   lista.Free;   ListBox1.Items.Add(s); end;