Search code examples
delphirandomnumbers

Randomize, choose random numbers from 1 to 6, but not 0


I'd like to make 2 dices, but I don't want it to choose 0, this is my code:

procedure TForm1.Button1Click(Sender: TObject);
var x1,x2:integer; text1,text2:string;
begin
randomize;
x1:=random(7);
x2:=random(7);

text1:=inttostr(x1);
text2:=inttostr(x2);

label1.Caption:=text1;
label2.Caption:=text2;

end;
end.

What should I do to make it choose from 1 to 6, without including 0?


Solution

  • x1:=random(6) + 1;
    

    should do the trick, it will never return zero now.