-4

I need to generate a rectangle with random dimentions, but I want to avoid generating any rectangles above a specified area limit. Notice I said area, I can't simply have a random value within a certain range be generated for each axis independently.

In practice I need to generate a set of two values that have a product within a certain range.

Heres kinda what i'd need:

//generates any 2 numbers that multiply to less then 30 and more then 0
randomArea = Vector2 Random.Area(0, maxArea)

(Answers preferably in C#)

  • What exactly are you having trouble with? If you already know what area you want, it shouldn't be too difficult. – Broots Waymb Aug 02 '21 at 14:22
  • 1
    Seems like you can generate one side/random and then determine what the max of the other side is and then generate that random. – JNevill Aug 02 '21 at 14:23
  • Thanks, i don't know how i didn't think of that – Brickolo _ Aug 02 '21 at 14:25
  • Note that this will not create an even distribution. To improve that you could alternate which side to create first.. – TaW Aug 02 '21 at 14:27
  • See following : Random rand = new Random(); const int MAX_AREA = 30; int width = rand.Next(1, 30); int height = rand.Next(1, MAX_AREA / width); – jdweng Aug 02 '21 at 14:28

1 Answers1

1

Randomly generate the area. Randomly generate one axis. Divide the area by the axis, and you will get the other axis.