Selecting a Percentage of a Collection Randomly in MAXScript

Posted Feb 3, 2016
Last Updated Feb 5, 2016

Sometimes you want to select a subset of objects randomly to apply a material or effect. Instead of doing it manually, it's best to write a script to do it. Here is an example MAXScript function to do just that.

First open the MAXScript Editor. Then paste the following code into it and evaluate the MAXScript by clicking Tools > Evaluate All or CTRL+E. Now you can call the function during the MAX session. You can also expand the MAXScript code into a fuller script that always calls the function on the current scene selection and bind it to a keyboard shortcut or button in your menus.

function getRandomFromCollection collection percentage rseed:123456789 = (
	seed rseed -- set the randomization seed
	local cCount = collection.count as float
	local dec = (percentage as float) / 100.0
	local cuttoff = cCount * dec
	local results = for obj in collection where ((random 1.0 cCount) <= cuttoff) collect obj

Now you can use this on any collection, from object selections to arrays of custom structs. You can use it in a script like this where objs is an array of objects in the scene and you want to randomly select 25% of the objects in the array:

select (getRandomFromCollection objs 25 rseed:(random 1 123345678))

If you want to keep re results predicable, pass a static rseed value.


  • This function does not guarantee that exactly the percentage of objects you specify will be returned. Instead, it will provide your percentage as an average over time. So if you pass a collection of 100 objects and request 25 percent, you will generally get 25 results--but sometimes get 24 or 26.
  • The smaller the collection, the less accurate the percentage result will be; the larger the collection, the more accurate.


No HTML Tags are permitted.

Angry Teapot Level Design Awards

Newsletter Subscription