473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

[Help]Random Walk

crystal2005
Hi, another thread from me ^_^

This time i am required to write a “random walk” function across a 10 x 10 array. The array will contain characters (all ‘.’ initially). Until this point, to the best of my knowledge, i just create 2D array and assign all [10][10] with '.' initial.

Expand|Select|Wrap|Line Numbers
  1.         for(i=0;i<10;i++)
  2.         {
  3.             for(j=0;j<10;j++)
  4.             {
  5.                 a[i][j] = '.';
  6.             }
  7.         }
  8.  

The function must randomly “walk” from element to element, always going up, down, left, or right by one element. The elements visited by the program will be labeled with the letters A through Z, in the order visited.

So the output should look like this

Expand|Select|Wrap|Line Numbers
  1. A . . . . . . . . .
  2. B C D . . . . . . .
  3. . F E . . . . . . .
  4. H G . . . . . . . .
  5. I . . . . . . . . .
  6. J . . . . . . . Z .
  7. K . . R S T U V Y .
  8. L M P Q . . . W X .
  9. . N O . . . . . . .
  10. . . . . . . . . . .
  11.  
At this point i'm completely clueless T_T

The only thing i can tell is the use of srand or rand function is a must to generate random numbers, However to use those randomly generated numbers, i have no idea. I hope someone can help me, thank you for any suggestion to get this program done.
Mar 28 '09 #1
2 5644
JosAH
11,448 Expert 8TB
There are a few subproblems to solve:

1) how to generate up/down/left or right randomly;

This one is easy: define two small arrays:

Expand|Select|Wrap|Line Numbers
  1. int dx[]= { 0, 0, -1, 1 } // x component of up/down/left/right
  2. int dy[]= { -1, 1, 0, 0 } // y component of up/down/left/right
  3. ...
  4. int x, y;
  5. int d= rand()%4; // pick a direction
  6.  
  7. int new_x= x+dx[d];
  8. int new_y= y+dy[d];
  9.  
The (new_x, new_y) position is selected randomly; now you have to solve:

2) is the new position on the board;

You can test the values for < 0 or > 9 but you can also add a one row/column border around your board not containing a dot '.' indicating that you can't move to that position; the empty board will look like this:

Expand|Select|Wrap|Line Numbers
  1. - - - - - - - - - - - - 
  2. - . . . . . . . . . . -
  3. - . . . . . . . . . . -
  4. - . . . . . . . . . . -
  5. - . . . . . . . . . . -
  6. - . . . . . . . . . . -
  7. - . . . . . . . . . . -
  8. - . . . . . . . . . . -
  9. - . . . . . . . . . . -
  10. - . . . . . . . . . . -
  11. - . . . . . . . . . . -
  12. - - - - - - - - - - - - 
  13.  
A dot '.' means a 'free' location, any other value means its a 'taken' value. Of course you only print values for coordinates in the range [1, 10] i.e. you never see that border. Doing it this way eliminates checking whether or not you are still 'on the board'.

Last you have to keep a counter, starting at 'A' and increment it until it reaches the value 'Z'. Those are the values you have to put on the board while moving.

Take care that you can get 'stuck', i.e.

Expand|Select|Wrap|Line Numbers
  1. - - - - - - - - - - - - 
  2. - A B C . . . . . . . -
  3. - H I D . . . . . . . -
  4. - G F E . . . . . . . -
  5. - . . . . . . . . . . -
  6. - . . . . . . . . . . -
  7. - . . . . . . . . . . -
  8. - . . . . . . . . . . -
  9. - . . . . . . . . . . -
  10. - . . . . . . . . . . -
  11. - . . . . . . . . . . -
  12. - - - - - - - - - - - - 
  13.  
You should be able to solve the rest of the details.

kind regards,

Jos
Mar 28 '09 #2
Thanks for the guidance... I'll try my best first :)
Mar 29 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Eric | last post by:
I'm a new programmer on Visual Basic and for any of you who play tank wars, I'm attempting to lay out a terrain similar to it. What I need is a terrain that I can draw (Basically just a random...
10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
9
by: hokieghal99 | last post by:
This script is not recursive... in order to make it recursive, I have to call it several times (my kludge... hey, it works). I thought os.walk's sole purpose was to recursively walk a directory...
7
by: has | last post by:
<BLUSH> Careless talk costs lives, as they say. In my case, a throwaway comment that Python could trounce the notoriously underpowered and undersupported AppleScript language for "serious number...
2
by: Calvin79 | last post by:
Hi All, I've just started to try python so forgive the ignorance! I've been using this, or something like it; First = random.choice ('abc') answer = first print answer - no problems there.
3
by: questions? | last post by:
How to generate a random number in Python. Is there any build in function I can call? Thanks
6
by: Steve Peterson | last post by:
Hi Probably a dumb question, but I need to generate a random number between 0 and 9. I was hoping that someone could help out with a code snipplet on how to do this. TIA Steve Peterson
3
by: ana10192000 | last post by:
import java.util.Random; public class random_game { public static void main (String args) { int ctr,guess; Random r = new Random(); System.int.println(r.input.int(100)); int...
1
by: jackspam | last post by:
I need help on getting started with a dice program that will output as many random numbers from 1 to 6 and as many rolls as the user requests, and then how many times each number shows up from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.