help making random integers.
Hi!!! I am making a game where i put a "circle" on a coordinate board. the board is size of 12 X 12, and i need to make sure that each circle(12 circles) lie on a distince space. So each circle has its coordinate (x1, y1), (x2, y2),... so on. How can i create a random integer for the coordinates to ensure that every circle lie on a distinc place on the board?
[368 byte] By [
MidoBana] at [2007-10-3 10:23:41]

Look at the Math API and Random API.
i know how to make random integers using math class.but what my concern is that, how can i make sure each random integer to be distinct, or at least if x1 == x2, then y1 != y2. or if y1 == y2, then x1!=x2 how can i do that?Message was edited by: MidoBan
Your requirements are contradictory.
First you state you want random numbers, then you state those numbers should not repeat.
The second requirement effectively cancels out the first, because truely random numbers DO repeat (or at least they have a chance to, as each number has an identical chance of popping up at each throw of the dice).
For your requirements, you'd probably best use Random.nextInt(int seed).
To make sure you don't generate the same number more than once, keep track of the numbers already generated and draw another random number if the one you got was drawn in the past.
class createSircel(){
drawSircel(getCoordinates(););
}
class getCoordinates(){
*here you do all ur x1 == x2, then y1 != y2. or if y1 == y2, then x1!=x2 stuff*
return x1 x2 y1 y2
}
class drawSircel(x1 x2 y1 y2){
* do ur paint*
}
/* dont know if this is right but this is the flow that i would of used prob*/
SvHa at 2007-7-15 5:45:28 >

Ok. I'll rephrase my words. What i really need to do is, on a 12 x 12 board(similar to chess board), i have to put 12 circles at a random place. with using (x, y) coordinates on that board.
So i thought, in order to do that i need to create 24 different random integers that fall within that 12 X12 board and put them. but i have to makes sure that each 12 circles lie on a distinct space.
And for this, don't worry about putting circle on the board and that GUI stuff. i only need help in making
those random integers.
Message was edited by:
MidoBan
do u want the logic of HOW to look at this or do u have an idea but ur struggling with the code?
SvHa at 2007-7-15 5:45:28 >

> Ok. I'll rephrase my words. What i really need to do
> is, on a 12 x 12 board(similar to chess board), i
> have to put 12 circles at a random place. with using
> (x, y) coordinates on that board.
>
> So i thought, in order to do that i need to create 24
> different random integers that fall within that 12
> X12 board and put them. but i have to makes sure that
> each 12 circles lie on a distinct space.
>
> And for this, don't worry about putting circle on the
> board and that GUI stuff. i only need help in making
>
> those random integers.
>
> Message was edited by:
> MidoBan
you can use the board to tell you if you've got a duplicate. basically, generate the random number, look at the square that correlates to and if it hasn't already got a piece on it, add one and increment some counter. when the counter reaches 24, you're done
i only know how to create random integer using Math.randombut that's the only thing i know. help me out with the logic.
"you can use the board to tell you if you've got a duplicate. basically, generate the random number, look at the square that correlates to and if it hasn't already got a piece on it, add one and increment some counter. when the counter reaches 24, you're done "
no... the board is given to us, and it can't have function like that. so i need to play around with this random numbers.
> "you can use the board to tell you if you've got a
> duplicate. basically, generate the random number,
> look at the square that correlates to and if it
> hasn't already got a piece on it, add one and
> increment some counter. when the counter reaches 24,
> you're done "
>
> no... the board is given to us, and it can't have
> function like that. so i need to play around with
> this random numbers.
you're saying you can't check the board to see where the pieces are? then how are you going to write a game?
the board has only method likedrawCircle(5, 9);then it draws circle on that spot.
so i'm creating another class to put these 12 circles on to that board class.
> the board has only method like> drawCircle(5, 9);> then it draws circle on that spot.so? you create a model of the board in, say, a 2-dimensional array, and then proceed as suggested above
so u cant touch the code given to u?
SvHa at 2007-7-15 5:45:28 >

this is what has to be done
public class DrawCircle
{
public static void main(String[] args)
{
Board myBoard = new Board(12, 12);
x1 = (int)(12 * Math.random()) +1;
y1 = (int)(12 * Math.random()) +1;
x2 = (int)(12 * Math.random()) +1;
y2 = (int)(12 * Math.random()) +1;
.
.
.
x12 = (int)(12 * Math.random()) +1;
y12 = (int)(12 * Math.random()) +1;
myBoard.drawCircle(x1, y1);
myBoard.drawCircle(x2, y2);
myBoard.drawCircle(x3, y3);
.
.
.
myBoard.drawCircle(x12, y12);
}
}
and i need to make sure that each circle is being put on a different place.
right. and there have been several different suggestions here for you to approach this problem. lots of luck
(im noob myself so bare with me i dunno if this is the right way but should work)
when a sircel is created call a class that u created lets say class SAVELOCATION
al that that class do is keep the track of all the occupied places
lets take an array for exmple. a sircel is drawn at 5,6
so add an entry in array "5,6"
this is done in SAVELOCATION
so when u drawsircel call SAVELOCATION and pass the 5,6 to it.
now thats saved. now create a class exmple
CHECKLOCATION
this is done before drawing to get a place thats not occupied.
pass the values that u want to draw to CHECKLOCATION
CHECKLOCATION will loop through the array and and compare the values stored with the one that u want to store. if no matches found return true. draw the sircel and SAVELOCATION. if false generate a new loaction and try again.
that logic should work. dont know if its the right way.
SvHa at 2007-7-21 13:13:43 >

This is easy, you must generate a pair of values X1,Y1 ok? then you generate another pair of values (x2,y2)
if(x2>=x1)
x2+=/*minim distance between centres*/
else
x2-=/*minim distance between centres*/
if(x1>=x1)
x1+=/*minim distance between centres*/
else
x1-=/*minim distance between centres*/
All numbers all random an never circles are superposed.