Questions about how to draw some random squares

D

digibooster

Hi, I am currently working on a program that draw buildings with some
random windows.

I figured out how to draw the buildings by set up the drawRect(x, y,
width, height), but I still have questions about how to randomize
windows (square shape) within those buildings. Do I need to setup
ranges for the windows? What is the method for setup random squares?
Any suggestion or comments are welcomed. Thank you
 
L

Luc The Perverse

Hi, I am currently working on a program that draw buildings with some
random windows.

I figured out how to draw the buildings by set up the drawRect(x, y,
width, height), but I still have questions about how to randomize
windows (square shape) within those buildings. Do I need to setup
ranges for the windows? What is the method for setup random squares?
Any suggestion or comments are welcomed. Thank you

Your question doesn't make sense

If you want a random square, choose a random width, and a random upper left
corner position. If you want it to "look like" something - then you need
some kind of limit on the randomness.
 
D

digibooster

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.
 
A

Andrew Thompson

Maybe I didn't make myself clear.

The original explanation managed to confuse at least two
people, lucky for you that one of them bothered to express
that, huh? ;-)
...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).

That is much better.
...The random part really got me.

You need either an instance of the Random class, or
Math.random(), to produce random numbers.
Math.random() is the easier to use..

boolean darkWindow = ( Math.random()>0.7 ) // gone to bed!

HTH

Andrew T.
 
D

digibooster

You need either an instance of the Random class, or
Math.random(), to produce random numbers.
Math.random() is the easier to use..

boolean darkWindow = ( Math.random()>0.7 ) // gone to bed!

Could you explain it in detail a little bit. I am still confused.
 
A

Andrew Thompson

Could you explain it in detail ..

Not here. This group is for people that can read javadocs
and ask specific questions with code examples(*)..
...a little bit. I am still confused.

Try comp.lang.java.help for more detailed help.

* That is not the charter, merely my personal view.
Anybody that disagrees with my personal view can feel
free to answer your question (here) themselves.

Andrew T.
 
D

digibooster

Thank you. I already had the feelings that I don't belong here, at
least for now. :D Appreciate your time though
 
L

Luc The Perverse

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
 
L

Luc The Perverse

Andrew Thompson said:
Try comp.lang.java.help for more detailed help.

* That is not the charter, merely my personal view.
Anybody that disagrees with my personal view can feel
free to answer your question (here) themselves.

I think your view is generally accepted
 
M

Mark Jeffcoat

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.

Are you saying that you wish someone would reply with a pointer
to java.util.Random? Or were you hoping for java.lang.Math.random()?

Because the only other answer I can think of is at
http://catb.org/~esr/faqs/smart-questions.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top