Files
hustoj/web/cppreference-zh/chmhelp/cpp-numeric-random-discrete_distribution-discrete_distribution.html
2025-08-01 12:57:54 +08:00

122 lines
9.0 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::discrete_distribution&lt;IntType&gt;::discrete_distribution</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_numeric_random_discrete_distribution_discrete_distribution skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::discrete_distribution&lt;IntType&gt;::</span>discrete_distribution</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-numeric.html">numeric</a>&lrm; | <a href="cpp-numeric-random.html">random</a>&lrm; | <a href="cpp-numeric-random-discrete_distribution.html">discrete distribution</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">discrete_distribution<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(1)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> InputIt <span class="sy1">&gt;</span><br />
discrete_distribution<span class="br0">(</span> InputIt first, InputIt last <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">discrete_distribution<span class="br0">(</span> <a href="cpp-utility-initializer_list.html"><span class="kw960">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><span class="kw4">double</span><span class="sy1">&gt;</span> weights <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(3)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> UnaryOperation <span class="sy1">&gt;</span><br /></span>
<p>discrete_distribution<span class="br0">(</span> <a href="cpp-types-size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> count, <span class="kw4">double</span> xmin, <span class="kw4">double</span> xmax,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UnaryOperation unary_op <span class="br0">)</span><span class="sy4">;</span></div>
</td>
<td>(4)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">explicit</span> discrete_distribution<span class="br0">(</span> <span class="kw4">const</span> param_type<span class="sy3">&amp;</span> params <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(5)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#26500;&#36896;&#26032;&#30340;&#20998;&#24067;&#23545;&#35937;&#12290;</p>
<p>1) &#40664;&#35748;&#20998;&#24067;&#20989;&#25968;&#12290;&#26500;&#36896;&#25317;&#26377;&#21333;&#20010;&#26435;&#37325; <span class="texhtml" style="white-space: nowrap;">p={1}</span> &#30340;&#20998;&#24067;&#12290;&#27492;&#20998;&#24067;&#22987;&#32456;&#29983;&#25104; <span class="t-c"><span class="mw-geshi cpp source-cpp">&#8203;<span class="nu0">0</span>&#8203;</span></span> &#12290;</p>
<p>2) &#26500;&#36896;&#25317;&#26377;&#33539;&#22260; <code>[first, last)</code> &#20013;&#26435;&#37325;&#30340;&#20998;&#24067;&#12290;&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">first <span class="sy1">==</span> last</span></span> &#65292;&#21017;&#25928;&#26524;&#21516;&#40664;&#35748;&#26500;&#36896;&#20989;&#25968;&#12290;</p>
<p>3) &#26500;&#36896;&#25317;&#26377; <code>weights</code> &#20013;&#26435;&#37325;&#30340;&#20998;&#24067;&#12290;&#31561;&#25928;&#22320;&#35843;&#29992; <span class="t-c"><span class="mw-geshi cpp source-cpp">discrete_distribution<span class="br0">(</span>weights.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span>, weights.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span></span></span> &#12290;</p>
4) &#26500;&#36896;&#25317;&#26377;&#20197;&#20989;&#25968; <code>unary_op</code> &#29983;&#25104;&#30340; <code>count</code> &#20010;&#26435;&#37325;&#30340;&#20998;&#24067;&#12290;&#27599;&#20010;&#26435;&#37325;&#31561;&#20110; <span class="texhtml" style="white-space: nowrap;">w<span class="t-su t-su-b"><br />
i</span> = unary_op(xmin + &delta;/2 + i &middot; &delta;)</span> &#65292;&#20854;&#20013; <span class="texhtml" style="white-space: nowrap;">&delta; = <span class="t-mfrac"></span></span>
<table>
<tr>
<td>(xmax &minus; xmin)</td>
</tr>
<tr>
<td>count</td>
</tr>
</table>
&#32780; <span class="texhtml" style="white-space: nowrap;">i &isin; {0, ..., count&minus;1}</span> &#12290; <code>xmin</code> &#19982; <code>xmax</code> &#24517;&#39035;&#20351;&#24471; <span class="texhtml" style="white-space: nowrap;">&delta; &gt; 0</span> &#12290;&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">count <span class="sy1">==</span> <span class="nu0">0</span></span></span> &#65292;&#21017;&#25928;&#26524;&#21516;&#40664;&#35748;&#26500;&#36896;&#20989;&#25968;&#12290;
<p>5) &#20197; <code>params</code> &#20026;&#20998;&#24067;&#21442;&#25968;&#26500;&#36896;&#20998;&#24067;&#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>first, last</td>
<td>-</td>
<td>&#23450;&#20041;&#29992;&#20316;&#26435;&#37325;&#30340;&#25968;&#30340;&#20803;&#32032;&#33539;&#22260;&#12290; <code>InputIterator</code> &#25152;&#25351;&#30340;&#20803;&#32032;&#31867;&#22411;&#24517;&#39035;&#21487;&#36716;&#25442;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span></td>
</tr>
<tr class="t-par">
<td>weights</td>
<td>-</td>
<td>&#21547;&#26435;&#37325;&#30340; initializer_list</td>
</tr>
<tr class="t-par">
<td>unary_op</td>
<td>-</td>
<td>&#23558;&#35201;&#24212;&#29992;&#30340;&#19968;&#20803;&#31639;&#31526;&#20989;&#25968;&#12290;<br />
&#20989;&#25968;&#31614;&#21517;&#24212;&#31561;&#20215;&#20110;&#22914;&#19979;&#32773;&#65306;
<p><span class="t-cc"><span class="mw-geshi cpp source-cpp">&nbsp;Ret fun<span class="br0">(</span><span class="kw4">const</span> Type <span class="sy3">&amp;</span>a<span class="br0">)</span><span class="sy4">;</span></span></span></p>
<p>&#31614;&#21517;&#19981;&#24517;&#26377; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">const</span> <span class="sy3">&amp;</span></span></span> &#12290;<br />
&#31867;&#22411; <span class="t-c"><span class="mw-geshi cpp source-cpp">Type</span></span> &#24517;&#39035;&#20351;&#24471; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span> &#31867;&#22411;&#23545;&#35937;&#33021;&#22312;&#35299;&#24341;&#29992;&#21518;&#38544;&#24335;&#36716;&#25442;&#21040; <span class="t-c"><span class="mw-geshi cpp source-cpp">Type</span></span> &#12290; &#31867;&#22411; <span class="t-c"><span class="mw-geshi cpp source-cpp">Ret</span></span> &#24517;&#39035;&#20351;&#24471; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span> &#31867;&#22411;&#23545;&#35937;&#33021;&#34987;&#35299;&#24341;&#29992;&#24182;&#33021;&#34987;&#36171; <span class="t-c"><span class="mw-geshi cpp source-cpp">Ret</span></span> &#31867;&#22411;&#20540;&#12290; &#8203;</p>
</td>
</tr>
<tr class="t-par">
<td>params</td>
<td>-</td>
<td>&#20998;&#24067;&#21442;&#25968;&#38598;</td>
</tr>
<tr class="t-par-hitem">
<td colspan="3">&#31867;&#22411;&#35201;&#27714;</td>
</tr>
<tr class="t-par-req">
<td colspan="3">-
<div class="spacer"></div>
<code>InputIt</code> &#24517;&#39035;&#28385;&#36275;<a href="cpp-named_req-InputIterator.html"><span style="font-style:italic">&#32769;&#24335;&#36755;&#20837;&#36845;&#20195;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(LegacyInputIterator)</span></a> &#30340;&#35201;&#27714;&#12290;</td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>