Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want those probabilities in the long run, you can just get values by randomly selecting one element from the array {1,2,2,3,3,3,4,4,4,4}.</p> <p>If you however need to retrieve exactly 1000 elements, in those specific quantities, you can try something like this (not C#, but shouldn't be a problem):</p> <pre class="lang-java prettyprint-override"><code>import java.util.Random; import java.util.*; class Thing{ Random r = new Random(); ArrayList&lt;Integer&gt; numbers=new ArrayList&lt;Integer&gt;(); ArrayList&lt;Integer&gt; counts=new ArrayList&lt;Integer&gt;(); int totalCount; public void set(int i, int count){ numbers.add(i); counts.add(count); totalCount+=count; } public int getValue(){ if (totalCount==0) throw new IllegalStateException(); double pos = r.nextDouble(); double z = 0; int index = 0; //we select elements using their remaining counts for probabilities for (; index&lt;counts.size(); index++){ z += counts.get(index) / ((double)totalCount); if (pos&lt;z) break; } int result = numbers.get(index); counts.set( index , counts.get(index)-1); if (counts.get(index)==0){ counts.remove(index); numbers.remove(index); } totalCount--; return result; } } class Test{ public static void main(String []args){ Thing t = new Thing(){{ set(1,100); set(2,200); set(3,300); set(4,400); }}; int[]hist=new int[4]; for (int i=0;i&lt;1000;i++){ int value = t.getValue(); System.out.print(value); hist[value-1]++; } System.out.println(); double sum=0; for (int i=0;i&lt;4;i++) sum+=hist[i]; for (int i=0;i&lt;4;i++) System.out.printf("%d: %d values, %f%%\n",i+1,hist[i], (100*hist[i]/sum)); } } </code></pre>
    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