<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Zufall Funktionen]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>Wie immer dreht sich bei mir um Zufall ^^</p>
<p>Diesmal geht es bei meiner Frage darum wie kann ich dem Prgramm sagen das er zwischen 2 oder mehreren Funktionensblöcken wählen soll, also zufällig. Dabei soll eine Funktion eine höhere Chance haben gewählt zu werden.</p>
<p>Beispiel Code Block A 60% : 40% Code Block B.</p>
<p>Ich dachte mir ich mache es mit einem Zufallsgenerator und weise Block A Zahl 1 und Block B Zahl 2 zu, aber da steht die Chance ja 50 : 50.</p>
<p>Meine andere Idee wäre Zufallsgenrator 1 - 10. 1 - 6 steht für A und 7 - 10 für B. Aber vielleicht gibt es bessere alternative?!</p>
<p>Ich möchte erwähnen das ich Anfänger bin und ihr mir es idioten sicher erklären müsstet ^^</p>
<p>Danke euch ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324713/zufall-funktionen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 20:59:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324713.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 28 Mar 2014 23:38:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zufall Funktionen on Fri, 28 Mar 2014 23:38:01 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>Wie immer dreht sich bei mir um Zufall ^^</p>
<p>Diesmal geht es bei meiner Frage darum wie kann ich dem Prgramm sagen das er zwischen 2 oder mehreren Funktionensblöcken wählen soll, also zufällig. Dabei soll eine Funktion eine höhere Chance haben gewählt zu werden.</p>
<p>Beispiel Code Block A 60% : 40% Code Block B.</p>
<p>Ich dachte mir ich mache es mit einem Zufallsgenerator und weise Block A Zahl 1 und Block B Zahl 2 zu, aber da steht die Chance ja 50 : 50.</p>
<p>Meine andere Idee wäre Zufallsgenrator 1 - 10. 1 - 6 steht für A und 7 - 10 für B. Aber vielleicht gibt es bessere alternative?!</p>
<p>Ich möchte erwähnen das ich Anfänger bin und ihr mir es idioten sicher erklären müsstet ^^</p>
<p>Danke euch ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391688</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391688</guid><dc:creator><![CDATA[Chechen65]]></dc:creator><pubDate>Fri, 28 Mar 2014 23:38:01 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Fri, 28 Mar 2014 23:50:47 GMT]]></title><description><![CDATA[<p>Ganz einfach: Zieh eine Zufallszahl im bereich 1-10 und wenn sie &lt;= 6 ist mach das eine, ansonsten das andere.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391689</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Fri, 28 Mar 2014 23:50:47 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Sat, 29 Mar 2014 00:46:17 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Ganz einfach: Zieh eine Zufallszahl im bereich 1-10 und wenn sie &lt;= 6 ist mach das eine, ansonsten das andere.</p>
</blockquote>
<p>Geht auch einfacher</p>
<pre><code>std::random_device dev;
std::mt19937 generator{ dev() };
std::bernoulli_distribution distribution{ 0.6 };
</code></pre>
<p>Etwas mehr Information: Der an den Konstruktor übergebene Wert stellt die Wahrscheinlichkeit dar, mit der der Ausdruck <code>distribution(generator)</code> den Wert <code>true</code> zurückgibt. Verwendung sieht so aus:</p>
<pre><code>static std::random_device dev;
static std::mt19937 generator{ dev() };
static std::bernoulli_distribution distribution{ 0.6 };

if( distribution(generator) ) // Wahrscheinlichkeit 60%
    /* ... */
else
    /* ... */
</code></pre>
<p>Bzw. man bastelt sich eine Funktion, die ein <a href="http://en.cppreference.com/w/cpp/utility/functional/bind" rel="nofollow">gebundenes</a> Objekt zurückgibt.</p>
<pre><code>#include &lt;random&gt;
#include &lt;iostream&gt;
#include &lt;functional&gt;

std::function&lt;bool()&gt; get_prng_bernoulli( double val )
{
	std::random_device dev;
	std::mt19937 gen{dev()};
	std::bernoulli_distribution d{val};
	return [=] () mutable { return d(gen); };
}

int main()
{
	auto p = get_prng_bernoulli(0.6);
	for( unsigned i = 0; i != 10; ++i )
		std::cout &lt;&lt; ( p()? '*' : '+' );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2391690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391690</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 29 Mar 2014 00:46:17 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Sat, 29 Mar 2014 00:07:33 GMT]]></title><description><![CDATA[<p>Ansonsten, speziell für &quot;kompliziertere&quot; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.43056em;"></span><span class="strut bottom" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathit">p</span></span></span></span>, kann man auch <a href="http://www.cplusplus.com/reference/random/bernoulli_distribution/" rel="nofollow">std::bernoulli_distribution</a> verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391691</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391691</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Sat, 29 Mar 2014 00:07:33 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Sat, 29 Mar 2014 00:08:40 GMT]]></title><description><![CDATA[<p>Chechen65 schrieb:</p>
<blockquote>
<p>Meine andere Idee wäre Zufallsgenrator 1 - 10. 1 - 6 steht für A und 7 - 10 für B. Aber vielleicht gibt es bessere alternative?!</p>
</blockquote>
<p>Jo, das geht. Es ginge auch mit Zufallszahl 1-5 und 1,2,3 für A und 4,5 für B. Oder mit 1-100 und 1-60 für A und 61-100 für B, etc.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391692</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391692</guid><dc:creator><![CDATA[brückekürzen]]></dc:creator><pubDate>Sat, 29 Mar 2014 00:08:40 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Sat, 29 Mar 2014 08:05:06 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Geht auch einfacher</p>
<pre><code>std::random_device dev;
std::mt19937 generator{ dev() };
std::bernoulli_distribution distribution{ 0.6 };
</code></pre>
</blockquote>
<p>Das wäre die korrekte Lösung, geht auch mit VS2013 Express, aber mit dem <a href="http://www.c-plusplus.net/forum/p2391348#2391348" rel="nofollow">VS2013 Ultimate von Chenchen nicht</a> (wohl weil er versehentlich einen Breakpoint gesetzt hat). Also wird er weiter schlechte Lösungen verwenden. Schon etwas deprimierend.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391709</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391709</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Sat, 29 Mar 2014 08:05:06 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall Funktionen on Sat, 29 Mar 2014 12:09:19 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Ganz einfach: Zieh eine Zufallszahl im bereich 1-10 und wenn sie &lt;= 6 ist mach das eine, ansonsten das andere.</p>
</blockquote>
<p><strong>Hab alternativen erwartet.</strong></p>
<p>icarus2 schrieb:</p>
<blockquote>
<p>Ansonsten, speziell für &quot;kompliziertere&quot; p, kann man auch std::bernoulli_distribution verwenden.</p>
</blockquote>
<p><strong>Danke, ich schau es mir an.</strong></p>
<p>Arcoth schrieb:</p>
<blockquote>
<p>Geht auch einfacher</p>
<pre><code>std::random_device dev;
std::mt19937 generator{ dev() };
std::bernoulli_distribution distribution{ 0.6 };
</code></pre>
</blockquote>
<p><strong>Ich habe die Funktion ausprobiert und bei mir kommt dauernd eine Fehlermeldung in der Zeile <em>std::random_device dev;</em>. Sowohl bei Visual Studio 2013 Ultimate als auch bei Dev-C++.</strong></p>
<p>nwp3 schrieb:</p>
<blockquote>
<p>Das wäre die korrekte Lösung, geht auch mit VS2013 Express, aber mit dem VS2013 Ultimate von Chenchen nicht (wohl weil er versehentlich einen Breakpoint gesetzt hat). Also wird er weiter schlechte Lösungen verwenden. Schon etwas deprimierend.</p>
</blockquote>
<p><strong>Gnenau, wie schon gesagt geht das auch nicht bei Dev-C++.<br />
Was kann das Problem sein?</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2391722</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2391722</guid><dc:creator><![CDATA[Chechen65]]></dc:creator><pubDate>Sat, 29 Mar 2014 12:09:19 GMT</pubDate></item></channel></rss>