Take the 2-minute tour ×

Can anyone suggest any links, ideas or algorithms to generate flowers randomly like the one as my profile pic? The profile pic flower has only a 10 x 10 grid and the algorithm is not truly random. I would also prefer that the new algorithm use a grid of about 500 x 500 or even better, allow the user to pick the size of the grid.

[Plant[][] is declared as int plant[10][10];]

public void generateSimpleSky(){

    for(int w2=0;w2<10;w2++)
        for(int w3=0;w3<10;w3++)


public void generateSimpleSoil(){

    for(int q=0;q<10;q++)


public void generateSimpleStem(){

    int ry=rand.nextInt(4);

    for(int u=7;u>1;u--){

        int yu=rand.nextInt(3);



public void generateSimpleFlower(){


    for(int q2=1;q2<4;q2++)

    for(int q3=xr-1;q3<=xr+1;q3++)

share|improve this question
What do you mean by truly random? –  Jeremy Jan 2 '13 at 3:01
I second @Nile 's comment. You might want to first isolate what about the flower you want to be random. color, petal size, core size/color, stem size, etc. A true randomization of that flower would make for a real messy plant. –  brainmurphy1 Jan 2 '13 at 3:06
I mean that the algorithm that i used to generate the flower in my profile pic used an algorithm than only changed the way the stem ooked. The flower still looked the same. So i want to make it "truly randam" i.e. make the enteire procedure random. –  Hele Jan 2 '13 at 3:07
@brainmurphy1: I was specifically referring to the difference between a true random number and a psuedorandom number, but I second your comment also. –  Jeremy Jan 2 '13 at 3:08
Did you have a look at L-Systems? en.wikipedia.org/wiki/L-system –  Henry Jan 3 '13 at 6:58
show 5 more comments

1 Answer

up vote 4 down vote accepted

It sounds like a reasonably simple problem where you just generate 1 parameter at a time, possibly based on the output of the previous variables.

My model of a flower will be: It has just a reasonably upright stem, a perfectly round center, some amount of leaves on the stem on alternating sides, petals perfectly distributed around the center.

random() is just a random number within some chosen bounds, the bounds may be unique for each variable. random(x1, x2, ..., xn) generates a random number within some bounds dependent on the variables x1, x2, ..., xn (as in stemWidth < stemHeight/2, a reasonable assumption).

The Stem

stemXPosition = width / 2
stemHeight = random()
stemWidth = random(stemHeight)
stemColour = randomColour()
stemWidthVariationMax = random(stemWidth, stemHeight)
stemWidthVariationPerPixel = random(stemWidth, stemHeight)

stemWidthVariationMax/-PerPixel are for generating a stem that isn't perfectly straight (if you want to do something that complicated, a low PerPixel is for smoothness). Generate the stem using these as follows:

pixelRelative[y-position][0] := left x-position at that y-position relative to the stem
pixelRelative[y-position][1] := right x-position at that y-position relative to the stem

pixelRelative[0][0] = randomInRange(-stemWidthVariationMax, stemWidthVariationMax)
for each y > 0:
  pixelRelative[y-1][0] = max(min(randomInRange(pixel[y] - stemWidthVariationPerPixel,
                                        pixel[y] + stemWidthVariationPerPixel),
//pixelRelative[0][1] and pixelRelative[y-1][1] generated same as pixelRelative[y-1][i]
for each y:
  pixelAbsolute[y][0] = width / 2 - stemWidth / 2 + pixelRelative[y][0]
  pixelAbsolute[y][1] = width / 2 + stemWidth / 2 + pixelRelative[y][1]

You can also use arcs to simplify things and go more than 1 pixel at a time.

The Top

centerRadius = random(stemHeight)
petalCount = random() // probably >= 3
petalSize = random(centerRadius, petalCount)

It's not too easy to generate the petals, you need to step from 0 to 2*PI with step-size of 2*PI/petalCount and generate arcs around the circle. It requires either a good graphics API or some decent maths.

Here's some nicely generated tops of flowers, though seemingly not open-source. Note that they don't have a center at all. (or centerRadius = 0)

The Leaves

You could probably write an entire paper on this, (like this one) but a simple idea would just be to generate a 1/2 circle and extend lines outward from there to meet at 2*the radius of the circle and to draw parallel lines on the flower.

Once you have a leaf generation algorithm:

leafSize = random(stemHeight) // either all leaves are the same size or generate the size for each randomly
leafStemLength = random(leafSize) // either all leaves have the same stem length or generate for each randomly
leafStemWidth = random(leafStemLength)
leaf[0].YPosition = random(stemHeight)
leaf[0].XSide = randomly either left or right
leaf[0].rotation = random between say 0 and 80 degrees
for each leaf i:
  leaf[i].YPosition = random(stemHeight, leaf[i-1]) // only generate new leaves above previous leaves
  leaf[i].XSide = opposite of leaf[i].XSide

Last words

The way to determine the bounds of each random would be either to argue it out, or give it some fixed value, generate everything else randomly a few times, keep increasing / decreasing it until it starts to look weird.

10 x 10 versus 500 x 500 would probably require greatly different algorithms, I wouldn't recommend the above for below 100 x 100, maybe generate a bigger image and simply shrink it using averaging or something.


I started writing some Java code, when I realised it may take a bit longer than I would like to spend on this, so I'll show you what I have so far.

  // some other code, including these functions to generate random numbers:
  float nextFloat(float rangeStart, float rangeEnd);
  int nextInt(int rangeStart, int rangeEnd);


  // generates a color somewhere between green and brown
  Color stemColor = Color.getHSBColor(nextFloat(0.1, 0.2), nextFloat(0.5, 1), nextFloat(0.2, 0.8));
  int stemHeight = nextInt(height/2, 3*height/4);
  int stemWidth = nextInt(height/20, height/20 + height/5);
  Color flowerColor = ??? // I just couldn't use the same method as above to generate bright colors, but I'm sure it's not too difficult
  int flowerRadius = nextInt(Math.min(stemHeight, height - stemHeight)/4, 3*Math.min(stemHeight, height - stemHeight)/4);
share|improve this answer
To put it simply, You're awesome! –  Hele Jan 4 '13 at 15:47
add comment

Your Answer


