Nick
Nick

Reputation: 137

Creating objects with random inputs

I have created a simple algorithm to solve a bin packing problem. To test it properly I need to create lots of box objects of various sizes (different lengths and widths). I have a box object: public Box(int width, int height) How would I create say 500 boxes without hard coding them of different sizes and store them in an ArrayList?

Thank you for your help

Upvotes: 1

Views: 39

Answers (1)

Bohemian
Bohemian

Reputation: 425238

Use the Random class, perhaps with a factory method:

public static Box create(int minWidth, int maxWidth, int minHeight, int maxHeight) {
    Random random = Random();
    return new Box(minWidth + random.nextInt(maxWidth - minWidth), minHeight+ random.nextInt(maxHeight - minHeight));
}

Upvotes: 2

Related Questions