<body class="vscode-light">
<h1 id="%e5%85%b6%e5%ae%9e%e5%b0%b1%e6%98%af%e7%ae%80%e5%8d%95%e7%9a%84%e5%b8%a6%e6%9d%83%e7%ae%97%e6%b3%95">其实就是简单的带权算法</h1>
<pre><code class="language-ruby"><div>@rets = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">15</span>]
@
quan = [<span class="hljs-number">10</span>, <span class="hljs-number">30</span>, <span class="hljs-number">50</span>, <span class="hljs-number">70</span>, <span class="hljs-number">100</span>]
@
randMax = @
quan.inject(<span class="hljs-symbol">:+</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">clacOneRet</span></span>
randV = rand(@randMax) + <span class="hljs-number">1</span>
@
quan.each_index <span class="hljs-keyword">do</span> <span class="hljs-params">|i|</span>
randV = randV - @
quan[i]
<span class="hljs-keyword">return</span> @
rets[i] <span class="hljs-keyword">if</span> randV <= <span class="hljs-number">0</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>
<span class="hljs-keyword">end</span>
nums = {}
@
rets.each {<span class="hljs-params">|k|</span> nums[k] = <span class="hljs-number">0</span>}
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>..<span class="hljs-number">1000</span>
v = clacOneRet
nums[v] = nums[v].to_i + <span class="hljs-number">1</span>
<span class="hljs-keyword">end</span>
puts nums <span class="hljs-comment">## {1=>31, 2=>123, 3=>182, 4=>263, 15=>401}</span>
</div></code></pre>