-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomPartition.m
More file actions
40 lines (29 loc) · 871 Bytes
/
Copy pathRandomPartition.m
File metadata and controls
40 lines (29 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(* ::Package:: *)
(* ::Section:: *)
(*Definition*)
(* ::Subsection:: *)
(*Wrap Function*)
(* ::Subsection:: *)
(*Main Functions*)
RandomPartition[l_List, max_] := RandomPartition[l, Array[1&, max] -> Range[1, max]];
RandomPartition[l_List, min_ ;; max_] := RandomPartition[l, Array[1&, max - min + 1] -> Range[min, max]];
RandomPartition[l_List, vList_List] := RandomPartition[l, Array[1&, Length@vList] -> vList];
RandomPartition[l_List, pList_List -> vList_List] := Block[
{p = DoPartition[Length@l, vList, pList]},
Internal`PartitionRagged[l, p]
];
(* ::Subsection:: *)
(*Auxiliary Functions*)
DoPartition[n_Integer, vList_, pList_] := Block[
{l = {}, r},
Do[
r = RandomChoice[pList -> vList];
Which[
Tr@l + r < n, AppendTo[l, r],
Tr@l + r == n, Return@Append[l, r],
True, Null
],
{i, Ceiling[n / Min@vList]}
];
Append[l, n - Tr@l]
]