| | |
help About Random
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
i have a class with 10 object and each object have a random position like (x,y)
now i wand a code that do not put object in same position
please complete my code
now i wand a code that do not put object in same position
please complete my code
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <time.h> #include <stdlib.h> class chess{ public : chess(); private : int x; int y; }; chess::chess() { x=rand()%15+1; //rand()%max+min will give values from min to min+max-1 y=rand()%15+1; } main() { chess a[10]; }
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
You need to set the rand seed value, use srand() . Unlike Java and C# the seed value, when not set, is the same every time it is run. I suggest using the current time to set the seed value. C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <time.h> #include <stdlib.h> class chess{ public : chess(); private : int x; int y; }; chess::chess() { x=rand()%15+1; //rand()%max+min will give values from min to min+max-1 y=rand()%15+1; } main() { srand((unsigned)time(NULL)); chess a[10]; }
Create a static member in your class which is a bool type 2D array (call it say,current_pos) of 8X8 elements.
Each time you assign a Position to the object, be sure to mark the current_pos[x][y]=TRUE;
So, now you can keep track of what all postition are been ocupied.
Hence, as soon as your random() generates a new position, check whether that position is already occupied or not. If it is, tell him to generate another number and so on.
Since the array will be a static member, its value will be same for all the object of the class.
Each time you assign a Position to the object, be sure to mark the current_pos[x][y]=TRUE;
So, now you can keep track of what all postition are been ocupied.
Hence, as soon as your random() generates a new position, check whether that position is already occupied or not. If it is, tell him to generate another number and so on.
Since the array will be a static member, its value will be same for all the object of the class.
Last edited by siddhant3s; Mar 1st, 2009 at 9:37 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
![]() |
Similar Threads
- Random number generator's (C++)
- Random Shutdowns (Troubleshooting Dead Machines)
- *Random System Restarts* (Win XP) (Windows NT / 2000 / XP)
- Random Restarts (Windows NT / 2000 / XP)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with Menu's
- Next Thread: program help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





