Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Choose random parts of an expression
ResourceFunction["RandomPartChoice"][expr] chooses a random subexpression from expr. | |
ResourceFunction["RandomPartChoice"][expr,count] chooses count subexpressions. | |
ResourceFunction["RandomPartChoice"][expr,count,levelspec] only considers subexpressions at levels given by levelspec. | |
ResourceFunction["RandomPartChoice"][expr,count,levelspec,wrapper] applies wrapper to each item before returning the result. | |
ResourceFunction["RandomPartChoice"][expr,count,levelspec,wrapper,pattern] only considers subexpressions that match pattern. |
Get a random part of an expression:
| In[1]:= |
| Out[1]= |
Get multiple parts:
| In[2]:= |
| Out[2]= |
Provide a level specification:
| In[3]:= |
| Out[3]= |
Apply a function to each item before returning:
| In[4]:= |
| Out[4]= |
Restrict sampling to elements that match a given pattern:
| In[5]:= |
| Out[5]= |
Use Automatic for the count to obtain the one-argument behavior when providing additional arguments:
| In[6]:= |
| Out[6]= |
Get random parts of an Association:
| In[7]:= |
| Out[7]= |
By default, the heads of expressions are not included in the sample space:
| In[8]:= |
| Out[8]= |
Allow choosing heads:
| In[9]:= |
| Out[9]= |
RandomPartChoice[expr,count,{1}] is equivalent to RandomChoice[expr,count]:
| In[10]:= |
| Out[9]= |
| In[11]:= |
| Out[12]= |
RandomPartChoice[expr,count] is equivalent to RandomChoice[Cases[expr,_,{0,Infinity}],count]:
| In[13]:= |
| Out[9]= |
| In[14]:= |
| Out[15]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License