943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 455
  • C++ RSS
Feb 27th, 2009
0

help About Random

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. class chess{
  6.  
  7. public :
  8. chess();
  9. private :
  10. int x;
  11. int y;
  12. };
  13.  
  14.  
  15.  
  16. chess::chess()
  17. {
  18. x=rand()%15+1; //rand()%max+min will give values from min to min+max-1
  19. y=rand()%15+1;
  20. }
  21.  
  22. main()
  23. {
  24. chess a[10];
  25. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aminpost is offline Offline
2 posts
since Feb 2009
Feb 27th, 2009
0

Re: help About Random

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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Feb 27th, 2009
0

Re: help About Random

Click to Expand / Collapse  Quote originally posted by nmaillet ...
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.
now what should i do?
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. class chess{
  6.  
  7. public :
  8. chess();
  9. private :
  10. int x;
  11. int y;
  12. };
  13.  
  14.  
  15.  
  16. chess::chess()
  17. {
  18. x=rand()%15+1; //rand()%max+min will give values from min to min+max-1
  19. y=rand()%15+1;
  20. }
  21.  
  22. main()
  23. {
  24. srand((unsigned)time(NULL));
  25. chess a[10];
  26. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aminpost is offline Offline
2 posts
since Feb 2009
Feb 27th, 2009
0

Re: help About Random

You can run it through a loop to check the other pieces to ensure that the randomly generated square is not already occupied.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Feb 28th, 2009
0

Re: help About Random

Help yes. Writing code for others, no, sorry ;P

Thanx...
Sean
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Mar 1st, 2009
0

Re: help About Random

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.
Last edited by siddhant3s; Mar 1st, 2009 at 9:37 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Menu's
Next Thread in C++ Forum Timeline: program help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC