Maybe I didn't make myself clear. Here is the thing, I need to design a
class that represents couple buildings. Within those buildings, I need
to put some random lightened windows (yellow squares). The random part
really got me.
Thank you for quoting, but please do not top post (notice how I put my reply
below your quoted text).
Ah - so you are drawing buildings, presumably with a number of windows, and
you want to make some of the windows appear to have a light on inside, and
you want the selection of which windows are lighted to be random? (If this
is not the case, then none of the rest of the post will apply)
That is easy - but you are not randomly placing boxes (for the placement of
the boxes is quite uniform), you are randomly selecting a color for the
windows, which are boxes which already exist, even if they are not drawn
yet.
Select which color you want the windows to be, and select what percentage
dark, and what percentage light you want to have.
Now - do you want it to be strict, or do you want it to be loose? True
random would mean, even if 20% of the windows, on average, were supposed to
be light; it would be theoretically possible that all the windows would be
light at once, even if ridiculously unlikely.
To do that is easy (pseudocode)
if(rand()>0.20)
color is dark
else
color is yellow
Where rand is a PRNG which returns numbers betwen 0 and 1. (I think you can
use java.util.Random.nextDouble) Now if you want exactly a percentage, or
number of the windows to be lighted at once, you will have to introduce some
kind of memory. Select windows randomly from a pool, and remove from the
pool as you select them. The chosen windows will be light, the rest dark.
You may be able to do a similar thing overwriting already drawn windows.
For this I would suggest a PRNG which returns between 0 and some very large
number, and just mod by the number of items you have left in the list, this
will give you a result between 0 and (n-1) where n is the number of items
in the list.
Here is some [pseudo] code (mostly java)
Window[] allMyWindows = new Window[numWins];
int count = numWins;
//initialize windows to something meaningful
int numColored = count * 10 / 50; //20 percent
Window[] colored = new Window[numColored];
Window[] uncolored = new Window[count - numColored];
for(int i = 0;i<numColored;i++){
int n = intRand() % count;
count--;
colored = allMyWindows [n];
//swap
allMyWindows [n] = allMyWindows [count];
allMyWindows [count] = colored;
}
for(int i = 0;i<count;i++)
uncolored = allMyWindows ;
for(Window c : colored)
DrawWindow(c, Yellow);
for(Window c : uncolored)
DrawWindow(c, Grey);
Where intRand is a PRNG as described before which returns an integer between
0 and X (where X is quite large) I believe you can use
java.util.Random.nextInt() for something like this
I'm sure there are other ways, but to me these are the two most
staightforward ways to go about it.
Note: There are many powerful tools in java. You may be able to do
everything I did using collections in very few lines of code. You could
probably shuffle a collection of windows and then just color the first X
number of windows, and then the rest are grey