Create number of random rectangles inside bigger outer rectangle without intersect each other

@jacob.small thanks for the clarification.

I initially tried this by placing into a code block but it does not run properly on my machine.

I think I have updated the core to Dynamo to the latest.

DO you mind checking on your machine if everything runs appropriately??

Best regards

It worked fine for me after addressing a warning stemming from copy-paste formatting issues…

Perhaps you have a package installed which has generated a namespace conflict? This resolves some of the more common ones which have been introduced of late. If you’re seeing a different result post a screenshot showing the warning.

The code:

def populateRectangles (srf,count) //start the definition
 {
    rectangles = [Imperative] //force impetrative code
    {
    	//start the attempts at 0;
        attempts = 0;

 		//an empty list for the results;
        results = [];

        while (DSCore.List.Count(results) < count && attempts < 10) //loop the below while we have less squares than desired or we have tried less than 10 times
        {
        	// add one to the attempt count to ensure we don't get in an infinite loop;
            attempts = attempts +1;

 			// generate a random coordinate system for the total count
            csSet =
               Surface.CoordinateSystemAtParameter(
                    srf,
                    Math.RandomList(count),
                    Math.RandomList(count)
                );

			// generate a random rectangle for the total count
            rects =
                Rectangle.ByWidthLength(
                    Math.RandomList(count/2),
                    Math.RandomList(count/2)
                );

			//patch the rectangle into a surface and set it into position at the coordiante sytsesm
            rects =
                Surface.ByPatch(
                    Autodesk.Geometry.Transform(
                        rects,
                        csSet
                    )
                );

            //join the new rectangles to the list of resutls
            rects = DSCore.List.Join([results, rects]);

			//an empty list to hold the results
            set = [];

        	//for every item in the list of rectangles perform the loop
            for (i in 0..DSCore.List.Count(rects))
            {
            	//set is equal to
                set =
                	//if the nth rectangle doesn't interssect a rectangle in the set already
					List.AllFalse(Autodesk.Geometry.DoesIntersect(rects[i],set))?
						//the rectangle added to the end of the set
                        DSCore.List.Join([set,rects[i]]):
                        //otherwise the existing set
                        set;

            };

			// set the results to the set
            results = set;
        };

        //return the results from the rectangles code
		return = results;

    };
    // flatten the list of rectangles after intersecting with the surface, ensuring a flat list and that all rectangles are in the original surface
    rectSet = DSCore.List.Flatten(Autodesk.Geometry.Intersect(rectangles, srf),-1);
    //get the count of rectangles from the start of the list, incase we asked for 10 but had 12 due to two loops, and clean any null values out incase we had 9 but asked for 10
	rects = List.Clean(rectSet[0..count-1],false);
	//return the rectangles list
	return = rects;
 };```

Hey @jacob.small I wanted to thank you for the updated code it seems to have no issues but when running I


get an empty list. Attached is how I used your code and called the function.

Thanks in advance.

My guess is this is a namespace conflict.

Put each of the lines into a new code block (not in a definition) and see if any of them complain about a class not existing or having multiple name spaces.