Solved

# Horse race simulation

Posted on 2003-02-21
Medium Priority
282 Views
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;
{----------------------------------print results to screen--------------------------------------------}
for Kounter := 1 to 3 do                 {print 1,2,3 place results}
Writeln('In ',Kounter,' Place Horse number ',HorseNum[Kounter]);

end.
0
Question by:Lfteye

LVL 101

Accepted Solution

mlmcc earned 120 total points
ID: 7997558
One way i have handled this in the past is to use the random numbers to shuffle the values.

Create an array for the 10 horses.
Load it in any order (1-10 works just fine)
Generate pairs of random number from 1 - 10, i & j
Swap the i and j elements
Repeat generate and swap some number of times.
Horses(1), (2), (3). example

Horses
1  2  3  4  5  6  7  8  9  10  generate 4 & 8
1  2  3  8  5  6  7  4  9  10  generate 2 & 7
1  7  3  8  5  6  2  4  9  10
.
.
.
9  4  6  5  2  3  1  8  10  7

Horses 9 - 4 - 6 finish 1-2-3

mlmcc
0

LVL 7

Expert Comment

ID: 7998565
mlmcc's code will work, of course.

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[succ(rnd(10))]);

where exchange swaps the two values.

then read the "finishing sequence" from index 1,2,3 as before.

...Bill
0

Author Comment

ID: 7999297
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;

end.
0

LVL 1

Expert Comment

ID: 7999681
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:)
0

Author Comment

ID: 7999835
I think I get the idea, works well :)
0

LVL 101

Expert Comment

ID: 8000995
Looks exactly like what I suggested.

mlmcc
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my humble opinion (IMHO), TouchDown from Symantec is the best in class for this type of application, but Symantec has end-of-lifed it and although one can keep using it, it will no longer be supported or upgraded.  Time to look for alternatives t…
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll

#### 569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.