# Is there a way to select n points/prims randomly based on number/perce

8 replies to this topic

### #1 magneto

magneto

Grand Master

• Members
• 1,336 posts
• Joined: 04-October 11
• Name:Ryan K

Posted 25 February 2012 - 04:57 AM

Say you have a geometry and want to select 50 random points/prims, or 20% of all points/prims randomly.

Is there a way to do this? I got it working to a point using the Sort SOP, but can I do it without sorting the existing points/prims?

Thanks

Edited by magneto, 25 February 2012 - 05:08 AM.

### #2 eetu

eetu

Illusionist

• Members
• 480 posts
• Joined: 30-May 07
• Location:Helsinki, Finland
• Name:eetu m

Posted 25 February 2012 - 05:45 AM

You can do it stochastically by using rand(\$PR) < 0.2 or rand(\$PR) < 50 / nprims() to select about 20% or 50 respectively. This won't usually get you _exactly_ the right amount, but the more primitives you have, the closer it will usually get.

If you need to get exactly the desired amount, you can use a random Sort SOP, select the first 50 or 20%, and then GroupTransfer the groups back to the original geometry. That way you won't mess up the original primitive order.

#### Attached Files

A shitty theory is better than no theory at all

### #3 combi

combi

Peon

• Members
• 3 posts
• Joined: 01-December 09
• Name:Nicolas Combecave

Posted 25 February 2012 - 06:44 AM

eetu, on 25 February 2012 - 05:45 AM, said:

You can do it stochastically by using rand(\$PR) < 0.2 or rand(\$PR) < 50 / nprims() to select about 20% or 50 respectively. This won't usually get you _exactly_ the right amount, but the more primitives you have, the closer it will usually get.

If you need to get exactly the desired amount, you can use a random Sort SOP, select the first 50 or 20%, and then GroupTransfer the groups back to the original geometry. That way you won't mess up the original primitive order.

Hello Eetu,

I looked at your example, and as I'm quite new to houdini, and willing to learn it a little bit, I was just wondering if just setting the group sop operation mode on "Group By Range", and setting select 1 of 5 to get exactly 20% (or 1 of 2 for 50%) was also an option?
I just came to it because when looking at the content of the groupTransfert sop, I was not sure the results weere good for the exactly_50 one...

combi

### #4 magneto

magneto

Grand Master

• Members
• 1,336 posts
• Joined: 04-October 11
• Name:Ryan K

Posted 25 February 2012 - 06:51 AM

Wow Eetu, amazing trick. You are the man

### #5 eetu

eetu

Illusionist

• Members
• 480 posts
• Joined: 30-May 07
• Location:Helsinki, Finland
• Name:eetu m

Posted 25 February 2012 - 11:04 AM

combi, on 25 February 2012 - 06:44 AM, said:

I was just wondering if just setting the group sop operation mode on "Group By Range", and setting select 1 of 5 to get exactly 20% (or 1 of 2 for 50%) was also an option?
I just came to it because when looking at the content of the groupTransfert sop, I was not sure the results weere good for the exactly_50 one...

Yeap, that would work. Although it would be a bit less flexible, e.g. if you wanted to have 0..1 slider to control how large a portion you wish to select.

If rand() is properly random, it should not matter whether you take the first 20% or every fifth. The bad part of being properly random is that there is nothing preventing getting numbers that are close to each other - resulting in clumping in the result. If anyone has good ideas on how to prevent it in a situation like this, I'd like to hear

Some thinking out aloud:

There is the nrandom() / NonDeterministic Random vex/vop function that can be used to get a mersenne twister or quasistratified random sequence, but I'm not sure how to get that in a useful way to an (especially) primitive selection.
Using it in an nrandom() < 0.2 fashion loses its special qualities (I think), one should first run some vex to get a sequence that could then be used as primitive IDs to select.

Another idea might be to scatter points onto the geometry and then somehow select the polygons the points end up in. (schnelli's uniform scatterer?)

Or, if you want 20% of the faces, you could break up the geometry into 20 chunks with a ForEach SOP and select 1 face from each chunk - a kind of bruteforce startified sampling.
A shitty theory is better than no theory at all

### #6 hopbin9

hopbin9

Houdini Master

• Members
• 803 posts
• Joined: 14-March 10
• Name:Hop Bin

Posted 25 February 2012 - 11:32 AM

Drop a Grid SOP and set Rows/Columns to 25 by 25
Drop a Group SOP and connect Grid
Set operation to "Group by Expression"
Use this expression

(rand(\$PR) * \$NPR) < (\$NPR * 0.20)

Where 0.20 represents a 20% grouping.

That's all there is to it. You can add a constant to rand(\$PR+123) to apply a random seed.

Help us reach 1,000 fans on facebook!

### #7 hopbin9

hopbin9

Houdini Master

• Members
• 803 posts
• Joined: 14-March 10
• Name:Hop Bin

Posted 25 February 2012 - 11:37 AM

eetu, on 25 February 2012 - 05:45 AM, said:

rand(\$PR) < 0.2

Ah, even better. \$NPR cancel each other out.

Help us reach 1,000 fans on facebook!

### #8 Macha

Macha

Grand Master

• Members
• 1,654 posts
• Joined: 23-July 08
• Location:The Small Big P
• Name:Marc ♥

Posted 25 February 2012 - 12:59 PM

If you want exactly a random 20% how about you just use the randomize sort trick and then in the group sop set max range to 0.2*\$N with the 0.2 possibly being a chanel reference to a slider below.

Edited by Macha, 25 February 2012 - 01:00 PM.

My Vimeo

improve side effects - use haskell

### #9 magneto

magneto

Grand Master

• Members
• 1,336 posts
• Joined: 04-October 11