help About Random

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 2
Reputation: aminpost is an unknown quantity at this point 
Solved Threads: 0
aminpost aminpost is offline Offline
Newbie Poster

help About Random

 
0
  #1
Feb 27th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: help About Random

 
0
  #2
Feb 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: aminpost is an unknown quantity at this point 
Solved Threads: 0
aminpost aminpost is offline Offline
Newbie Poster

Re: help About Random

 
0
  #3
Feb 27th, 2009
Originally Posted by nmaillet View Post
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: help About Random

 
0
  #4
Feb 27th, 2009
You can run it through a loop to check the other pieces to ensure that the randomly generated square is not already occupied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: help About Random

 
0
  #5
Feb 28th, 2009
Help yes. Writing code for others, no, sorry ;P

Thanx...
Sean
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: help About Random

 
0
  #6
Mar 1st, 2009
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC