954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delphi Code help with the "Randomized Walk: Ant"

The question is as follows:

How long – number of steps -- does it take a drunken ant to fall off the edge of a square table if it starts in the middle? Perform a simulation to find out.

Imagine an ant in the middle of a table with, say, about 100 direct steps to any edge.
Each step the Ant takes is a random change of –1, 0, +1 in the X direction and of -1, 0, +1 in the Y direction, so the Ant can move to any one of nine positions relative to where it is (including falling down in its current position when the change in X is 0 and the change in Y is also 0).

How does one go about this?!

bogut
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

1) Decide algorithm
2) Try it on paper
3) Make test cases
4) Write code incrementally and run tests
5) Run and get results, when tests pass

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Try to make something like that

procedure RandomMove;
const
MoveX : array[0..2] of integer = (-1, 0, 1);
MoveY : array[0..2] of integer = (-1. 0, 1);
begin
Randomize; //you need to use this procedure before you the Random() one
Ant.MoveToXY(Random(MoveX), Random(MoveY)); //the value in Random() must be the value that will be randomized.
end;
AceStryker
Light Poster
47 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
 

The Randomize procedure task is to set the RandSeed constant to generate always random numbers...so it is need realy. :-D

FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: