Wolfram Research

Function Repository Resource:

RandomPartChoice

Source Notebook

Choose random parts of an expression

Contributed by: Richard Hennigan (Wolfram Research)

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.

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 SeedRandom.
A Method option to SeedRandom 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 Association objects that appear in expr.
The default level specification for ResourceFunction["RandomPartChoice"] is {0,Infinity}, with HeadsFalse.
ResourceFunction["RandomPartChoice"] uses standard level specifications:
n levels 1 through n
Infinity levels 1 through Infinity
{n} level n only
{n1,n2} 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]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{1 + x^2, 5, x^4, a + (1 + x^2)^2}]
Out[1]=

Get multiple parts:

In[2]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[x], g[a, b], h[c, d]}, 10]
Out[2]=

Provide a level specification:

In[3]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[x], g[a, b], h[c, d]}, 5,
  1]
Out[3]=

Apply a function to each item before returning:

In[4]:=
ResourceFunction["606d4f46-9ed8-442a-9149-2decde5643d3"][
 Hold[1 + 1, 2 + 2, 3 + 3], 3, 1, HoldForm]
Out[4]=

Restrict sampling to elements that match a given pattern:

In[5]:=
ResourceFunction["606d4f46-9ed8-442a-9149-2decde5643d3"][
 Hold[1 + 1, 2 + 2, 3 + 3], Automatic, Automatic, HoldForm, _Plus]
Out[5]=

Scope

Use Automatic for the count to obtain the one-argument behavior when providing additional arguments:

In[6]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[x], g[a, b], h[c, d]}, Automatic, 1]
Out[6]=

Get random parts of an Association:

In[7]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][<|1 -> 1 + x^2, 2 -> <|"a" -> x^2|>, 3 -> x^4, 4 -> a + (1 + x^2)^2|>, 5]
Out[7]=

Options

Heads

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

In[8]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[a], g[b], h[c]}[
  x], 10, {1, Infinity}]
Out[8]=

Allow choosing heads:

In[9]:=
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[a], g[b], h[c]}[
  x], 10, {1, Infinity}, Heads -> True]
Out[9]=

Properties and Relations

RandomPartChoice[expr,count,{1}] is equivalent to RandomChoice[expr,count]:

In[10]:=
SeedRandom[1];
RandomChoice[Range[10], 5]
Out[9]=
In[11]:=
SeedRandom[1];
ResourceFunction["606d4f46-9ed8-442a-9149-2decde5643d3"][
 Range[10], 5, {1}]
Out[12]=

RandomPartChoice[expr,count] is equivalent to RandomChoice[Cases[expr,_,{0,Infinity}],count]:

In[13]:=
SeedRandom[1];
RandomChoice[Cases[{f[x], g[a, b], h[c, d]}, _, {0, Infinity}], 5]
Out[9]=
In[14]:=
SeedRandom[1];
ResourceFunction[
 "606d4f46-9ed8-442a-9149-2decde5643d3"][{f[x], g[a, b], h[c, d]}, 5]
Out[15]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information