Function Repository Resource:

RandomPartition

Source Notebook

Cut a list into random segments

Contributed by: Aster Ctor

ResourceFunction["RandomPartition"][list,max]

divides list into smaller pieces of length no more than max.

ResourceFunction["RandomPartition"][list,min;;max]

divides list into smaller pieces of length min to max.

ResourceFunction["RandomPartition"][list,v]

divides list into smaller pieces with lengths chosen from integer list v.

ResourceFunction["RandomPartition"][list,pv]

divides list into smaller pieces with lengths chosen from list v according to weights p.

Examples

Basic Examples (1) 

Divide a list of length 10 into smaller pieces of length no more than 4:

In[1]:=
ResourceFunction["RandomPartition"][Range@10, 4]
Out[1]=

Scope (3) 

Divide a list of length 10 into small pieces of lengths 2 to 4:

In[2]:=
ResourceFunction["RandomPartition"][Range@20, 2 ;; 4]
Out[2]=

Divide a list of length 10 into small pieces of length 2 or 4:

In[3]:=
ResourceFunction["RandomPartition"][Range@20, {2, 4}]
Out[3]=

Divide a list of length 10 into small blocks of length 2 or 4 according to the specified weights:

In[4]:=
ResourceFunction["RandomPartition"][Range@20, {0.2, 0.8} -> {2, 4}]
Out[4]=

Possible Issues (1) 

If the given set cannot be segmented as requested, the last elements will break the limit instead of being lost:

In[5]:=
ResourceFunction["RandomPartition"][Range@10, {3}]
Out[5]=

Publisher

Aster Ctor (MoeNet)

Version History

  • 1.0.0 – 26 December 2019

Related Resources

License Information