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

## Recommended Posts

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
##### Share on other sites

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.

random_selections.hip

##### Share on other sites

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

##### Share on other sites

Wow Eetu, amazing trick. You are the man

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

rand(\$PR) < 0.2

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

##### Share on other sites

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
##### Share on other sites

Thanks a lot guys, I will have to try these new methods before I can comment, but all good info

##### Share on other sites

• 5 years later...

i am looking for an addition to above soutions. i am trying to create few random groups but exclusive to each other... (no member of one group should  belong to any other group)

in  term of percentage = for example gr1 has 10% random points... gr2 will have 10% of whats left after gr1 and so on and so forth.
in terms of number = G1 has 10 points and G2 will have 10 points but not any from G1 should be included... so on and so forth.

d

##### Share on other sites

17 hours ago, demoan666 said:

i am looking for an addition to above soutions. i am trying to create few random groups but exclusive to each other... (no member of one group should  belong to any other group)

in  term of percentage = for example gr1 has 10% random points... gr2 will have 10% of whats left after gr1 and so on and so forth.
in terms of number = G1 has 10 points and G2 will have 10 points but not any from G1 should be included... so on and so forth.

d

Ok so this feels tremendously hacky, but it seems to work  This feels like something that can be heavily collapsed into something smarter, but this seems to work if you need something now. It does mean you have to make as many attribcopys as you need groups, which doesn't feel very nice. It also means you have to add more if statements for each incremented selection, which I also don't like. You could always collapse it into a HDA with some switch sops and hide all the mess but that's not very procedural if you need more selections and groups than the HDA can handle. Another thing which I couldn't figure out is why say, a 20% selection wouldn't select exactly 20% of the points, despite fitting the return of rand to a 0-100 range and casting it to an integer. Originally I was just directly testing a floating slider against the return of rand() but even then with a setting of 0.2 the error persisted.

You can look through it all but the yellow nodes are where you define the group selection size at each step. Obviously because each subsequent group cannot select points already selected, if you set it to 100% on the first wrangle you won't have any points to feed into following groups.

If I get a chance, or if anyone else chimes in, I will endeavor to find a better way  Need the Vex practice anyway.

randomselection_01.hip

##### Share on other sites

thanks for taking the time to look into it. appreciate a lot  - noob in houdini (no programming language knowledge either) but i am liking it nonetheless!

thanks d.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×