PDA

View Full Version : selecting random elements in an array collection - help beginner


Fender-Hugh
09-09-2009, 07:14 PM
Hi all , i`m starting with MaxScritp and i`m having some doubts about the best or possible way to select a random number elements in an array say theArray1 from 1-array.count and then selecting randomly the items . For instance in a 10*10 PointHelpers in a scene collect the helpers.position into an array -> theArray1 , afterwards choosing a random number between 1 and theArray.count = Value , and then collect Value objects in the existing array.

So each time it would select a random set of the existing Helpers.pos

Thank you for the patience on such a beginners doubts ;)

denisT
09-09-2009, 08:20 PM
Hi all , i`m starting with MaxScritp and i`m having some doubts about the best or possible way to select a random number elements in an array say theArray1 from 1-array.count and then selecting randomly the items . For instance in a 10*10 PointHelpers in a scene collect the helpers.position into an array -> theArray1 , afterwards choosing a random number between 1 and theArray.count = Value , and then collect Value objects in the existing array.

So each time it would select a random set of the existing Helpers.pos

Thank you for the patience on such a beginners doubts ;)


fn randomPick list count:1 = if iskindof list Array do
(
if count >= list.count then list
else
(
local items = #()
while count > 0 do
(
i = random 1 list.count
append items list[i]
deleteitem list i
count -= 1
)
items
)
)


here is the function which returns an array of randomly picked elements from the list. Number of elements to pick is defined by count...

DreaTawn
09-09-2009, 08:20 PM
If you're after a completely random selection something like this should work.


select (for i in Array1 where random 0 1 == 1 collect i)
Cheers,

Drea

Fender-Hugh
09-10-2009, 03:57 PM
Thankx alot , suprisingly, yesteraday night I arrived to something very similar to denisT`s definition approach , code :

rollout setfield "RandomPoints"
(
button garray "getArray"
spinner NPoints "NumberPoints :" type: #integer range:[10,1000,50]
button SlPoints "SelectPoints"

on garray pressed do

(

theArray = $Helpers as array
blank = #()

for i=1 to NPoints.value do(
indic = random 1 theArray.count
appendifUnique blank theArray[indic]

)
)

on SlPoints pressed do (

select blank


)

)
fieldFloater = newRolloutFloater "Randomizer" 250 100
addRollout setField fieldFloater

This allowed me to specify a number of elements to collect , which should be equal to the number of times the core block inserts random items to a blank array for the final collect and select operation . But now I`m gonna see if i can match the number of loops with the final array.count since I`m using appendifunique only unique items are being retrieved ,soo i end specifying say 50 in the rollout yet the random code may return identical items making the final collection only 38 for example .

Anyway thanks alot you guys for lending a hand to a begginer , who was very happy knowing that "training is paying off" . Hugh

CGTalk Moderation
09-10-2009, 03:57 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.