44 lines
672 B
Plaintext
44 lines
672 B
Plaintext
ClearAll[sofncreator]
|
|
sofncreator[n_] := Module[{sample, i},
|
|
sample = {};
|
|
i = 0;
|
|
Return[
|
|
Function[{item},
|
|
i++;
|
|
If[i <= n,
|
|
AppendTo[sample, item]
|
|
,
|
|
If[RandomInteger[{1, i}] <= n,
|
|
sample[[RandomInteger[{1, n}]]] = item
|
|
]
|
|
];
|
|
sample
|
|
]
|
|
]
|
|
]
|
|
bin = ConstantArray[0, 10];
|
|
items = Range[10];
|
|
sofn = sofncreator[3];
|
|
Do[
|
|
sample = sofn[item];
|
|
Print[" Item: ", item, " -> sample: " , sample]
|
|
,
|
|
{item, items}
|
|
]
|
|
Do[
|
|
sofn = sofncreator[3];
|
|
Do[
|
|
sample = sofn[item]
|
|
,
|
|
{item, items}
|
|
];
|
|
Do[
|
|
bin[[s]] += 1
|
|
,
|
|
{s, sample}
|
|
]
|
|
,
|
|
{trial, 100000}
|
|
];
|
|
{Range[Length[bin]], bin} // Transpose // Grid
|