Lfteye
asked on
Horse race simulation
For practice and to learn I made up a little program that generates random numbers between 1 and 10 to simulate 1st,2nd and 3rd place horses in a race.The problem was duplicate numbers coming up can't have two number 3's etc.I'm new to this and I only do it for fun.My question is how would the experts do this.
{----------------My code---------------------- ------}
program RaceHorse(output);
var
HorseNum : Array[1..3] of Integer; {an array to hold horse numbers}
Kounter: byte;
begin
Randomize;
Writeln(' Race #',Random(9)); {print race number}
Writeln('----------------- --------') ;
for Kounter := 1 to 3 do
begin
HorseNum[Kounter]:= Random(9)+1; {assign a random number between 1 an 10 to array[index]}
{------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -------}
{Check to see if HorseNum[Kounter] contains the number about to be placed at this index}
if (HorseNum[Kounter-1] = HorseNum[Kounter]) or (HorseNum[Kounter-2] = HorseNum[Kounter]) then
HorseNum[Kounter] := HorseNum[Kounter]+1;
{------------------------- ---------- --------}
{repeat the adjustment for cases like num1 =4 num2 =5 3rd pick is going to be 4 so if statement increments(1)
making it a 5 still a duplicate choice. Running if statement twice seems to (virtually) eliminate this }
if (HorseNum[Kounter-1] = HorseNum[Kounter]) or (HorseNum[Kounter-2] = HorseNum[Kounter]) then
HorseNum[Kounter] := HorseNum[Kounter]+1;
end;
{------------------------- ---------p rint results to screen-------------------- ---------- ---------- ----}
for Kounter := 1 to 3 do {print 1,2,3 place results}
Writeln('In ',Kounter,' Place Horse number ',HorseNum[Kounter]);
Readln();
end.
{----------------My code----------------------
program RaceHorse(output);
var
HorseNum : Array[1..3] of Integer; {an array to hold horse numbers}
Kounter: byte;
begin
Randomize;
Writeln(' Race #',Random(9)); {print race number}
Writeln('-----------------
for Kounter := 1 to 3 do
begin
HorseNum[Kounter]:= Random(9)+1; {assign a random number between 1 an 10 to array[index]}
{-------------------------
{Check to see if HorseNum[Kounter] contains the number about to be placed at this index}
if (HorseNum[Kounter-1] = HorseNum[Kounter]) or (HorseNum[Kounter-2] = HorseNum[Kounter]) then
HorseNum[Kounter] := HorseNum[Kounter]+1;
{-------------------------
{repeat the adjustment for cases like num1 =4 num2 =5 3rd pick is going to be 4 so if statement increments(1)
making it a 5 still a duplicate choice. Running if statement twice seems to (virtually) eliminate this }
if (HorseNum[Kounter-1] = HorseNum[Kounter]) or (HorseNum[Kounter-2] = HorseNum[Kounter]) then
HorseNum[Kounter] := HorseNum[Kounter]+1;
end;
{-------------------------
for Kounter := 1 to 3 do {print 1,2,3 place results}
Writeln('In ',Kounter,' Place Horse number ',HorseNum[Kounter]);
Readln();
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok mimcc this is what i came up with is this what you mean?
{------------------------- -code----- ---------- ------}
program HorseRace2(output);
var
HorseNum : Array[1..10] of Byte; {an array to hold horse numbers}
Kounter,i,j,temp: byte;
begin
Randomize;
for Kounter := 1 to 10 do
HorseNum[Kounter] := Kounter;
for Kounter := 1 to 10 do
begin
{switch random elements 10 times-------------------}
i := Random(10)+1; {i = random number 1-10 say 3}
j := Random(10)+1; {j = random number 1-10 say 5}
temp := HorseNum[i] ; {3}
HorseNum[i] := HorseNum[j]; {3 = 5 i holds 5}
HorseNum[j] := temp; { 5 = 3 j holds 3 elements (i & j are switched)}
end;
for Kounter := 1 to 10 do
begin
Writeln('In ',Kounter,' Place Horse#..',HorseNum[Kounter ]);
end;
Readln;
end.
{-------------------------
program HorseRace2(output);
var
HorseNum : Array[1..10] of Byte; {an array to hold horse numbers}
Kounter,i,j,temp: byte;
begin
Randomize;
for Kounter := 1 to 10 do
HorseNum[Kounter] := Kounter;
for Kounter := 1 to 10 do
begin
{switch random elements 10 times-------------------}
i := Random(10)+1; {i = random number 1-10 say 3}
j := Random(10)+1; {j = random number 1-10 say 5}
temp := HorseNum[i] ; {3}
HorseNum[i] := HorseNum[j]; {3 = 5 i holds 5}
HorseNum[j] := temp; { 5 = 3 j holds 3 elements (i & j are switched)}
end;
for Kounter := 1 to 10 do
begin
Writeln('In ',Kounter,' Place Horse#..',HorseNum[Kounter
end;
Readln;
end.
ok my way:
type tab=array[1..3] of integer;
var t:tab;
x,i:integer;
function findDuplo(x:integer;t:tab) :boolean;
var i:integer;
p:boolean;
begin
i:=1;
p:=boolean(0);
while ( (i<=3) and NOT p ) do
begin
if t[i]=x then p:=boolean(1);
inc(i);
end;
findDuplo:=p;
end;
begin
randomize;
i:=1;
while i<=3 do
begin
x:=randomize(9+1);
if NOT findduplo(x,t) then
begin
t[i]:=x;
inc(i);
end;
end.
the 3 integer array is the winers list..
i might be wrong.. (missunderstood your intructions:)
type tab=array[1..3] of integer;
var t:tab;
x,i:integer;
function findDuplo(x:integer;t:tab)
var i:integer;
p:boolean;
begin
i:=1;
p:=boolean(0);
while ( (i<=3) and NOT p ) do
begin
if t[i]=x then p:=boolean(1);
inc(i);
end;
findDuplo:=p;
end;
begin
randomize;
i:=1;
while i<=3 do
begin
x:=randomize(9+1);
if NOT findduplo(x,t) then
begin
t[i]:=x;
inc(i);
end;
end.
the 3 integer array is the winers list..
i might be wrong.. (missunderstood your intructions:)
ASKER
I think I get the idea, works well :)
Looks exactly like what I suggested.
Glad to help
mlmcc
Glad to help
mlmcc
A simpler method along the same line, starting with the same array filled with 1..10 is,
for loop := 1 to 10 do
exchange(array[loop],array
where exchange swaps the two values.
then read the "finishing sequence" from index 1,2,3 as before.
...Bill