# RandomPartChoice

Contributed by: Richard Hennigan (Wolfram Research)

Choose random parts of an expression

 ResourceFunction["RandomPartChoice"][expr] choose a random subexpression from expr. ResourceFunction["RandomPartChoice"][expr,count] choose count subexpressions. ResourceFunction["RandomPartChoice"][expr,count,levelspec] only consider subexpressions at levels given by levelspec. ResourceFunction["RandomPartChoice"][expr,count,levelspec,wrapper] apply wrapper to each item before returning the result. ResourceFunction["RandomPartChoice"][expr,count,levelspec,wrapper,pattern] only consider subexpressions that match pattern.

## Details and Options

ResourceFunction["RandomPartChoice"][expr] chooses with equal probability between all of the subexpressions of expr.
ResourceFunction["RandomPartChoice"] gives a different sequence of pseudorandom choices whenever you run the Wolfram Language. You can start with a particular seed using .
A option to can be given to specify the pseudorandom generator used.
ResourceFunction["RandomPartChoice"][expr,count,levelspec,wrapper,pattern] tests all the subparts of expr in turn to try and find ones that match pattern.
ResourceFunction["RandomPartChoice"] looks only for matches to values in objects that appear in expr.
The default level specification for ResourceFunction["RandomPartChoice"] is {0,}, with HeadsFalse.
ResourceFunction["RandomPartChoice"] uses standard level specifications:
 n levels 1 through n Infinity levels 1 through Infinity { n } level n only { n 1 , n 2 } levels n1 through n2
A positive level n consists of all parts of expr specified by n indices.
A negative level -n consists of all parts of expr with depth n.
Level 0 corresponds to the whole expression.

## Examples

### Basic Examples

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]=

### Scope

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]=

### Options

By default, the heads of expressions are not included in the sample space:

 In[8]:=
 Out[8]=

 In[9]:=
 Out[9]=

### Properties and Relations

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]=