473,401 Members | 2,139 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,401 software developers and data experts.

random movement

1
I have made an object move across the screen from right to left in a straight line using a for loop. However now I need to use the random function to make it appear to move up and down randomly as it moves to the right of the screen, and where in the loop do I do this? any help starting this, thanks
Oct 20 '06 #1
1 5085
arne
315 Expert 100+
I have made an object move across the screen from right to left in a straight line using a for loop. However now I need to use the random function to make it appear to move up and down randomly as it moves to the right of the screen, and where in the loop do I do this? any help starting this, thanks
You could generate a random number and then decide from this number whether you go up,down,left or right. Like

Expand|Select|Wrap|Line Numbers
  1.         switch( rand() % 4 ) {
  2.  
  3.             case 0: 
  4.                 // go up
  5.                 cout << "up" << endl;
  6.                 break;
  7.             case 1:
  8.                 // go down
  9.                 cout << "down" << endl;
  10.                 break;
  11.             case 2:
  12.                 // go left
  13.                 cout << "left" << endl;
  14.                 break;
  15.             default:
  16.                 // go right
  17.                 cout << "right" << endl;
  18.                 break;
  19.                }    
  20.  
If you only need to decide about two directions, calculate rand()%2 and decide from that.
One loop iteration is one step of your object, so the above switch statement should be inside the loop.
Oct 20 '06 #2

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

Similar topics

14
by: Paul C-T | last post by:
Hi, Is there a way to repeat a set of code until a certain if statement is satisfied. If it is then to exit the loop and if not repeat the code? Say I want to write a card game application in...
4
by: Pjotr Wedersteers | last post by:
Does anyone know how one can get values from the on-chip random device on modern Intel processors using PHP ? I am using a windows (XP) based system, PHP 4.3.8 TIA Pjotr
2
by: Robin Shuff | last post by:
Hi, I'm trying to limit the movement of the mouse cursor in using a VB app. The idea is to stop the cursor straying on to the second monitor of a dual screen set-up (i.e. a projector) while this...
1
by: Rocky A | last post by:
I don't remember ever reading about this in any of the "how to" books I've got or I wouldn't bother you guys (honest, I'm not being lazy here.....OK maybe just a little, but you guys are SOOOO...
24
by: Tuvas | last post by:
Okay, I'm working on devoloping a simple, cryptographically secure number, from a range of numbers (As one might do for finding large numbers, to test if they are prime). My function looks like...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
1
by: sircool | last post by:
hi guys, the problemm is that you have a horse on the chess board.You should write a function that calculate the horse's movement.I mean how many movement later you can go every point on the board.I...
4
by: mike | last post by:
I have the opportunity to rescue a project that uses a mouse to sense the relative position of a machine. The hardware is built...just needs to be programmed. Stop snickering!!! I didn't do it...I...
5
by: hurricane_number_one | last post by:
I am creating a simple server application, that will listen for incoming mouse coordinates and then move the mouse accordingly. So basically it's like a very simple VNC server without and screen...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.