Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have an evenly distributed partition generator.</p> <p>Where n := the integer to be partitioned, r:= the number of slices: The algorithm is a patched version of the naive method of simply inserting partings at random. The problem with this method, as it appeared to me when I looked at its output, was that scenarios where partings are placed in the same spot are less likely to occur. There is only one way to get {1,1,1}, while there are 3! ways of getting {2,4,9}, any of {4,2,9},{2,4,9},{9,4,2}... will lead to the same partition placement when sorted. This has been amended by providing additional explicit opportunities for repeats. For each parting insertion, there's a chance that the position of the parting wont be random, but will be selected as a repeat of a formerly selected value. This balances the uneven probability distribution of the naive method right out.</p> <p>I have proved by exhaustion that each partitioning is perfectly equally likely for r = 3, n = 2. I cbf proving it for higher values but healfhearted ventures to do so found only promising signs. I also tested it on random input, finding that it is at least roughly even for every values I tried[but probably perfectly even].</p> <p>here it is in C++11: [the output format is different to what you're expecting, it's the positions of the partings rather than the size of the space between them. The conversion is easy, though]</p> <pre><code>#include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;random&gt; #include &lt;cassert&gt; template &lt;typename Parting, typename Seed&gt; vector&lt;Parting&gt; partitionGen(unsigned nparts, unsigned bandw, Seed seed){//nparts is the number of parts, that is, one greater than the number of dividers listed in the output vector. Bandw is the integer being partitioned. assert(nparts &gt; 0); vector&lt;Parting&gt; out(nparts-1); srand(seed); unsigned genRange = bandw; for(auto i=out.begin(); i&lt;out.end(); ++i, ++genRange){ unsigned gen = rand()%genRange; *i = ((gen&lt;bandw)? gen: *(i-(gen-bandw+1))); } sort(out.begin(), out.end(), less&lt;Parting&gt;()); return out; } </code></pre> <p>I don't like the fact that I have to sort it though. If Vlody's version has an even distribution, it appears that it'd be better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload