### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: The random function ...  (Read 28880 times)

#### GoDxNero

• Jr. Member
• Posts: 58
##### The random function ...
« on: October 17, 2009, 01:06:50 pm »
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;
« Last Edit: October 17, 2009, 01:09:22 pm by GoDxNero »

#### Martin_fr

• Hero Member
• Posts: 6622
• Debugger - SynEdit - and more
##### Re: The random function ...
« Reply #1 on: October 17, 2009, 01:32:17 pm »
do you call randomize ?

You should call it once when your app starts.

#### GoDxNero

• Jr. Member
• Posts: 58
##### Re: The random function ...
« Reply #2 on: October 17, 2009, 02:08:14 pm »
Oh , didnt know about that   .

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;
« Last Edit: October 17, 2009, 02:11:21 pm by GoDxNero »

#### Leledumbo

• Hero Member
• Posts: 8266
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: The random function ...
« Reply #3 on: October 19, 2009, 07:52:33 am »
You just need to call it once (at OnCreate for example), and it will hold throughout the program.

#### GoDxNero

• Jr. Member
• Posts: 58
##### Re: The random function ...
« Reply #4 on: October 23, 2009, 11:09:06 pm »
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

• Hero Member
• Posts: 1609
##### Re: The random function ...
« Reply #5 on: October 23, 2009, 11:17:08 pm »
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.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

#### GoDxNero

• Jr. Member
• Posts: 58
##### Re: The random function ...
« Reply #6 on: October 23, 2009, 11:20:39 pm »
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

• Global Moderator
• Hero Member
• Posts: 1891
##### Re: The random function ...
« Reply #7 on: October 23, 2009, 11:58:51 pm »
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

• Hero Member
• Posts: 6622
• Debugger - SynEdit - and more
##### Re: The random function ...
« Reply #8 on: October 24, 2009, 12:19:37 am »
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 .

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

• Jr. Member
• Posts: 58
##### Re: The random function ...
« Reply #9 on: October 24, 2009, 12:21:45 am »
I will try to bash my brain out with Theo code xD

#### davesimplewear

• Sr. Member
• Posts: 307
##### Re: The random function ...
« Reply #10 on: October 24, 2009, 10:00:57 am »
Library RxNew contains rxDice, why not checkout their code, why reinvent the wheel?
All things considered insanity seems the best option

#### brunello

• New member
• Posts: 5
##### Re: The random function ...
« Reply #11 on: October 24, 2009, 10:39:30 am »
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