<?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[Ziegenproblem - Denkfehler?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein Programm geschrieben, welches sich mit dem Ziegenproblem befasst.<br />
Es soll Bewiesen werden, dass das 'Wechseln' der Tür am klügsten ist.<br />
Nun zu meinem Problem, wenn das Programm 9.000.000.000 Versuche durchführt erhalte ich eine Erfolgsquote von ~66%. Bei 10.000.000.000 Versuchen ist die Erfolgsquote jedoch nurnoch 16%... wor könnte mein Denkfehler liegen?</p>
<p>PS: Mein Coding-Style ist nicht wirklich der beste, könnt mir ja Verbesserungsvorschläge geben.</p>
<p>#include &lt;iostream&gt;<br />
#include &lt;cstdlib&gt;<br />
#include &lt;time.h&gt;<br />
using namespace std;</p>
<pre><code class="language-cpp">int main()
{
    srand(time(0));
    long int versuche = 0;
    float erfolge = 0;
    int auto1;
    int tor[3];
    int wahl;

    while (versuche &lt; 10000000)
    {
        auto1 = rand()%3+1; // Tor mit Auto wird ausgesucht

        for (int i = 1; i &lt; 3; i++) // Tore einrichten
        {
            tor[i] = 0;
            if (i == auto1)
            {
                tor[i] = 1;
            }
        }

        wahl = rand()%3+1; //Tor-Wahl generieren

        for (int i = 1; i &lt; 3; i++) // Tor &quot;oeffnen&quot;
        {
            if (i != wahl &amp;&amp; tor[i] != 1)
            {
                tor[i] = 2;
            }
        }

        for (int i =1;i &lt; 3;i++) // Tor wechseln
        {
            if (tor[i] != 2 &amp;&amp; i != wahl)
            {
                wahl = i;
            }
        }

        if (wahl == auto1) // Erfolg auswerten
        {
            erfolge++;
        }

        versuche++;
    }

    cout &lt;&lt; &quot;Versuche: &quot; &lt;&lt; versuche &lt;&lt; endl;
    cout &lt;&lt; &quot;Erfolg in Prozent: &quot; &lt;&lt; erfolge*100/versuche &lt;&lt; &quot;%&quot; &lt;&lt; endl;
    cin  &gt;&gt; erfolge;

return 0;
}
</code></pre>
<p>Grüße,<br />
Ziege</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/268422/ziegenproblem-denkfehler</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 20:09:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/268422.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 08 Jun 2010 20:54:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Tue, 08 Jun 2010 20:54:52 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein Programm geschrieben, welches sich mit dem Ziegenproblem befasst.<br />
Es soll Bewiesen werden, dass das 'Wechseln' der Tür am klügsten ist.<br />
Nun zu meinem Problem, wenn das Programm 9.000.000.000 Versuche durchführt erhalte ich eine Erfolgsquote von ~66%. Bei 10.000.000.000 Versuchen ist die Erfolgsquote jedoch nurnoch 16%... wor könnte mein Denkfehler liegen?</p>
<p>PS: Mein Coding-Style ist nicht wirklich der beste, könnt mir ja Verbesserungsvorschläge geben.</p>
<p>#include &lt;iostream&gt;<br />
#include &lt;cstdlib&gt;<br />
#include &lt;time.h&gt;<br />
using namespace std;</p>
<pre><code class="language-cpp">int main()
{
    srand(time(0));
    long int versuche = 0;
    float erfolge = 0;
    int auto1;
    int tor[3];
    int wahl;

    while (versuche &lt; 10000000)
    {
        auto1 = rand()%3+1; // Tor mit Auto wird ausgesucht

        for (int i = 1; i &lt; 3; i++) // Tore einrichten
        {
            tor[i] = 0;
            if (i == auto1)
            {
                tor[i] = 1;
            }
        }

        wahl = rand()%3+1; //Tor-Wahl generieren

        for (int i = 1; i &lt; 3; i++) // Tor &quot;oeffnen&quot;
        {
            if (i != wahl &amp;&amp; tor[i] != 1)
            {
                tor[i] = 2;
            }
        }

        for (int i =1;i &lt; 3;i++) // Tor wechseln
        {
            if (tor[i] != 2 &amp;&amp; i != wahl)
            {
                wahl = i;
            }
        }

        if (wahl == auto1) // Erfolg auswerten
        {
            erfolge++;
        }

        versuche++;
    }

    cout &lt;&lt; &quot;Versuche: &quot; &lt;&lt; versuche &lt;&lt; endl;
    cout &lt;&lt; &quot;Erfolg in Prozent: &quot; &lt;&lt; erfolge*100/versuche &lt;&lt; &quot;%&quot; &lt;&lt; endl;
    cin  &gt;&gt; erfolge;

return 0;
}
</code></pre>
<p>Grüße,<br />
Ziege</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909209</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909209</guid><dc:creator><![CDATA[Ziege]]></dc:creator><pubDate>Tue, 08 Jun 2010 20:54:52 GMT</pubDate></item><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Tue, 08 Jun 2010 20:58:26 GMT]]></title><description><![CDATA[<blockquote>
<p>PS: Mein Coding-Style ist nicht wirklich der beste, könnt mir ja Verbesserungsvorschläge geben.</p>
</blockquote>
<p>Alle Variablen so lokal wie möglich machen.<br />
Ist der Fehler noch da, wenn Du mit double oder mit long long zählst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909210</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909210</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 08 Jun 2010 20:58:26 GMT</pubDate></item><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Tue, 08 Jun 2010 21:08:42 GMT]]></title><description><![CDATA[<blockquote>
<p>Alle Variablen so lokal wie möglich machen.<br />
Ist der Fehler noch da, wenn Du mit double oder mit long long zählst?</p>
</blockquote>
<p>Tatsächlich, lag an der Variable 'erfolge' double statt float und der Fehler ist behoben. War die Ganze Zeit überzeugt, es würde an der Variable 'versuche' liegen...</p>
<p>Danke volkard</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909214</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909214</guid><dc:creator><![CDATA[ziege]]></dc:creator><pubDate>Tue, 08 Jun 2010 21:08:42 GMT</pubDate></item><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Wed, 09 Jun 2010 06:00:10 GMT]]></title><description><![CDATA[<p>Wieso ist die Anzahl von Erfolgen eine Fließkommazahl.</p>
<p>Wenn Du etwas im Stil verbessern möchtest, würde ich als erstes mal Anfangen die Zustände der Tore (0,1,2) in irgendetwas lesbares zu kapseln. Und dann sehen, ob du auf ein paar Schleifen verzichten kannst.<br />
<code>for (int i = 1; i &lt; 3; i++)</code> läuft übrigens nur durch tor1-2. Das erste Tor ist tor[0].</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909267</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Wed, 09 Jun 2010 06:00:10 GMT</pubDate></item><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Wed, 09 Jun 2010 12:48:10 GMT]]></title><description><![CDATA[<p>Die Variable 'erfolge' ist eine Fließkommazahl, damit die Prozentangabe später mit Nachkommastellen angezeigt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909457</guid><dc:creator><![CDATA[Ziege]]></dc:creator><pubDate>Wed, 09 Jun 2010 12:48:10 GMT</pubDate></item><item><title><![CDATA[Reply to Ziegenproblem - Denkfehler? on Wed, 09 Jun 2010 13:03:50 GMT]]></title><description><![CDATA[<p>Ziege schrieb:</p>
<blockquote>
<p>Die Variable 'erfolge' ist eine Fließkommazahl, damit die Prozentangabe später mit Nachkommastellen angezeigt wird.</p>
</blockquote>
<p>Keine gute Idee. Die Anzahl der Erfolge ist naturgemäß ganzzahlig<sup>*</sup> und das sollte in einem Programm daher auch so repräsentiert werden. Wenn du an irgendeiner Stelle Ausgabe als Kommazahl haben willst, dann rechne an dieser Stelle in eine Kommazahl um anstatt dein ganzes Programm damit zu belasten. Umrechnen kann man dies entweder direkt durch einen static_cast auf double oder indirekt durch die Tatsache, dass Multiplikation und Division mit einer Fließkommazahl wieder in einer Fließkommazahl resultieren:</p>
<pre><code class="language-cpp">// Beispielsweise so:
cout &lt;&lt; &quot;Erfolg in Prozent: &quot; &lt;&lt; erfolge*100.0/versuche &lt;&lt; &quot;%&quot; &lt;&lt; endl; // 100 ist nun ein double -&gt; Ergebnis ist auch ein double
// Oder so:
cout &lt;&lt; &quot;Erfolg in Prozent: &quot; &lt;&lt; static_cast&lt;double&gt;(erfolge)*100/versuche &lt;&lt; &quot;%&quot; &lt;&lt; endl; // erfolge wird als double aufgefasst -&gt; Ergebnis ist auch ein double
</code></pre>
<p><sup>*</sup>: Und immer positiv! Also unsigned Datentyp nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1909462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1909462</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 09 Jun 2010 13:03:50 GMT</pubDate></item></channel></rss>