?
Solved

Horse race simulation

Posted on 2003-02-21
6
Medium Priority
?
281 Views
Last Modified: 2010-04-16
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]);
      Readln();

end.
0
Comment
Question by:Lfteye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 101

Accepted Solution

by:
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

by:billious
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

by:Lfteye
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;
     Readln;

end.
0
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 
LVL 1

Expert Comment

by:3ler
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

by:Lfteye
ID: 7999835
I think I get the idea, works well :)
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8000995
Looks exactly like what I suggested.  

Glad to help

mlmcc
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

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

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

Join & Ask a Question