# How get 10 random number

• 02-25-2009
aminpost
How get 10 random number
i have 10 object (a[10]) that they have int x and int y in their class.
now i want get number for x and y with rand()(min=1 and max=15)
and now each object have a position like (3,7)

i need a function (or two) that find same position and if finded give new position and then check for same position again

Code:

```#include <iostream.h> #include <stdlib.h> class chess{ public :         chess();  //constructor private:         int x;         int y; }; main() { chess a[10]; } chess::chess()  //get position { x=rand()%15+1; //rand()%max+min will give values from min to min+max-1 y=rand()%15+1; }```
• 02-25-2009
matsp
a[9] is a 9 element array!

Aside from that, you probably have to post your code for us to tell you what is going wrong.

--
Mats
• 02-25-2009
aminpost
Quote:

Originally Posted by matsp
a[9] is a 9 element array!

Mats

a[0]-a[1]-a[2]-...-a[9]=10 element array (you do not count a[0])
• 02-25-2009
phantomotap
O_o

int a[9]; // 9 element array
int b[10]; // 10 element array
b[9]; // access 10th element of 10 element array

Soma
• 02-25-2009
matsp
And if you actually want help with your random number problem, you still need to post the code, regardless what you think your arrays should be declared as.

--
Mats
• 02-25-2009
aminpost
first say sorry you are right

i need a function that find same position and if finded give new position and then check for same position again

Code:

```#include <iostream.h> #include <time.h> #include <stdlib.h> class chess{ public :         chess();  //constructor private:         int x;         int y; }; main() { srand((unsigned)time(NULL)); chess a[10]; } chess::chess()  //get position { x=rand()%15+1; //rand()%max+min will give values from min to min+max-1 y=rand()%15+1; }```
• 02-25-2009
matsp
Ok, so your current code always gets the same set of random numbers because you are not calling srand() at he start of your program.

The second problem is that you need to track which positions have been taken. There are many different ways, but the most obvious is to have say a 2D array that covers 1..15 (or 0..14) in both dimensions, and set it all to false/0, then when a position is taken, set it to true/1. So once a pair of coordinates have been picked, check if that position is already taken and pick a new one.

--
Mats
• 02-25-2009
aminpost
Quote:

Originally Posted by matsp

So once a pair of coordinates have been picked, check if that position is already taken and pick a new one.

--
Mats

they position are (x,y)
my problem is here,how?
code of function?
• 02-25-2009
matsp
Quote:

Originally Posted by aminpost
they position are (x,y)
my problem is here,how?
code of function?

So how DO YOU think it should be done. I have described the overall mechanism, so it's just the implementation details that matter here. And I think you should be able to do that yourself.

--
Mats
• 02-25-2009
aminpost
Quote:

Originally Posted by matsp
So how DO YOU think it should be done. I have described the overall mechanism, so it's just the implementation details that matter here. And I think you should be able to do that yourself.

--
Mats

i can check to find same position but after find and get new position it should be check again to find same position my problem is here
• 02-25-2009
anon
Code:

```do {     (x, y) = draw_a_pair_of_random_numbers(); } while (these_numbers_already_exist(x, y));```
This might be the basic loop. Put that into a for loop to repeat 10 times.
• 02-26-2009
aminpost
who can find problem ?

Code:

```#include <iostream.h> #include <time.h> #include <stdlib.h> class chess{ public :         chess();  //constructor private :         int x;         int y; }; main() { srand((unsigned)time(NULL)); chess a[10]; } chess::chess()  //get position in constructor { int k; do{ x=rand()%15+1; //rand()%max+min will give values from min to min+max-1 y=rand()%15+1; for(int j=1 ; j<=10 ; j++) {         k=0;         if(a[j].x==x && a[j].y==y)         break;         else         k=1; } } while(k); }```
• 02-26-2009
matsp
You have the basic idea right. However that implementation is well dodgy - you are checking positions of internal components of an object that hasn't been constructed yet (except for the last object - assuming you fix the loop as suggested below, that is).

You are also starting at index 1 and going to index 10 - which we discussed earlier, that if you have 10 elements, they have index from 0 to 9.

Finally, I don't think it's a good idea for the constructor to know that there are 10 objects of it's own kind being constructed - it is breaking the basic principles of the way constructors work. You should either:
1. Call the constrructor with validated x & y coordinates.
2. Have a separate map of the coordinates taken [as I suggested originally].
[Other valid alternatives also exist].

[1] You MAY find that if you are filling ALL cells of a range that you should "pick the empty" instead of using random for the last few items - that way, you don't get an infinite loop if the random number generator isn't capable of giving you ALL the positions in your matrix - but I doubt that is a problem right now.

--
Mats