<?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[Pi-Berechnung durch Zufallspfeilwürfe]]></title><description><![CDATA[<p>Hallo!<br />
Ich habe einen Algorithmus, der pi dadurch berechnet, indem er Pfeilwürfe in einen Vierteleinheitsquadrats zufällig &quot;wirft&quot; und dann das Verhältnis der Pfeilwürfe innerhalb des Vierteleinheitskreises durch die Gesamtanzahl mit dem Verhältnis zwischen der Fläche innerhalb des Vierteleinheitskreises durch das des Quadrats gleichsetzt.<br />
Das Problem ist nun, dass jedesfall die Zufallszahl so dämlich gewählt werden, dass für Näherungswert von pi 3,8 irgendwas rauskommt. Hier der Code:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;

double hoch(double a,int b)                      
     {
         double c=a; // Zwischenspeichern von a
         if (b==0) // a ^ 0 = 1
         return 1; // ...
         while(b&gt;1) // (b-1)-Mal
         {
                c*=a; // a ist alstes a Mal erstes a
                b--;
         }
         return c; // gibt Ergebnis zurück
     }  

/*
rand()Zahl Stellen rausfinden und 0, machen.
*/
double zufallszahl(int z)
    {
       int i=0,zneu;
       zneu=z;
       while((zneu/10)&gt;0)
       {
                zneu=zneu/10;
                i++;
       }
       double zfinal;
       zfinal=static_cast&lt;double&gt;(z)/static_cast&lt;double&gt;((hoch(10,i+1)));
       return zfinal;
}

int main()
{
	int ipfeilwuerfe,ianzahlin=0,ianzahlout=0;
	double ipi;
	double izufallx,izufally;
	srand(time(NULL));
	cout&lt;&lt;&quot;\nWieviel Pfeilwuerfe?\n&quot;;
	cin&gt;&gt;ipfeilwuerfe;
	cin.get();
	for (int i=1;i&lt;=ipfeilwuerfe;i++)
	{
    izufallx=zufallszahl(rand());
	izufally=zufallszahl(rand());
	if (sqrt(hoch(izufallx,2)+hoch(izufally,2))&lt;=1)
	{
       ianzahlin++;
       //cout&lt;&lt;izufallx&lt;&lt;&quot; &quot;&lt;&lt;izufally&lt;&lt;&quot; &quot;;cout&lt;&lt;&quot; drinne\n&quot;;
    }
    else
    {
        ianzahlout++;
        cout&lt;&lt;hoch(izufallx,2)+hoch(izufally,2)&lt;&lt;&quot;draussen\n&quot;;
    }
    }
    ipi=static_cast&lt;double&gt;(4*ianzahlin)/static_cast&lt;double&gt;(ipfeilwuerfe);
    cout&lt;&lt;ipi&lt;&lt;&quot;\n&quot;;
    cin.get();

}
</code></pre>
<p>Kann mir jemand sagen was da falsch ist?</p>
<p>Vielen Dank im Voraus<br />
eginobili20</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/141901/pi-berechnung-durch-zufallspfeilwürfe</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 16:14:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/141901.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Mar 2006 11:26:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Pi-Berechnung durch Zufallspfeilwürfe on Sun, 26 Mar 2006 11:26:52 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich habe einen Algorithmus, der pi dadurch berechnet, indem er Pfeilwürfe in einen Vierteleinheitsquadrats zufällig &quot;wirft&quot; und dann das Verhältnis der Pfeilwürfe innerhalb des Vierteleinheitskreises durch die Gesamtanzahl mit dem Verhältnis zwischen der Fläche innerhalb des Vierteleinheitskreises durch das des Quadrats gleichsetzt.<br />
Das Problem ist nun, dass jedesfall die Zufallszahl so dämlich gewählt werden, dass für Näherungswert von pi 3,8 irgendwas rauskommt. Hier der Code:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;
#include &lt;cmath&gt;

double hoch(double a,int b)                      
     {
         double c=a; // Zwischenspeichern von a
         if (b==0) // a ^ 0 = 1
         return 1; // ...
         while(b&gt;1) // (b-1)-Mal
         {
                c*=a; // a ist alstes a Mal erstes a
                b--;
         }
         return c; // gibt Ergebnis zurück
     }  

/*
rand()Zahl Stellen rausfinden und 0, machen.
*/
double zufallszahl(int z)
    {
       int i=0,zneu;
       zneu=z;
       while((zneu/10)&gt;0)
       {
                zneu=zneu/10;
                i++;
       }
       double zfinal;
       zfinal=static_cast&lt;double&gt;(z)/static_cast&lt;double&gt;((hoch(10,i+1)));
       return zfinal;
}

int main()
{
	int ipfeilwuerfe,ianzahlin=0,ianzahlout=0;
	double ipi;
	double izufallx,izufally;
	srand(time(NULL));
	cout&lt;&lt;&quot;\nWieviel Pfeilwuerfe?\n&quot;;
	cin&gt;&gt;ipfeilwuerfe;
	cin.get();
	for (int i=1;i&lt;=ipfeilwuerfe;i++)
	{
    izufallx=zufallszahl(rand());
	izufally=zufallszahl(rand());
	if (sqrt(hoch(izufallx,2)+hoch(izufally,2))&lt;=1)
	{
       ianzahlin++;
       //cout&lt;&lt;izufallx&lt;&lt;&quot; &quot;&lt;&lt;izufally&lt;&lt;&quot; &quot;;cout&lt;&lt;&quot; drinne\n&quot;;
    }
    else
    {
        ianzahlout++;
        cout&lt;&lt;hoch(izufallx,2)+hoch(izufally,2)&lt;&lt;&quot;draussen\n&quot;;
    }
    }
    ipi=static_cast&lt;double&gt;(4*ianzahlin)/static_cast&lt;double&gt;(ipfeilwuerfe);
    cout&lt;&lt;ipi&lt;&lt;&quot;\n&quot;;
    cin.get();

}
</code></pre>
<p>Kann mir jemand sagen was da falsch ist?</p>
<p>Vielen Dank im Voraus<br />
eginobili20</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024069</guid><dc:creator><![CDATA[eginobili20]]></dc:creator><pubDate>Sun, 26 Mar 2006 11:26:52 GMT</pubDate></item><item><title><![CDATA[Reply to Pi-Berechnung durch Zufallspfeilwürfe on Sun, 26 Mar 2006 15:11:30 GMT]]></title><description><![CDATA[<p>Ich vermute, das die Funktion 'zufallszahl' keine gleichverteilten Zufallszahlen im Intervall [0,1] liefert.<br />
Versuch' mal so was wie das:</p>
<pre><code class="language-cpp">double zufallszahl()
{
     int z = rand();
     double zfinal = double( z ) / RAND_MAX;
     return zfinal;
}
</code></pre>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024237</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024237</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 26 Mar 2006 15:11:30 GMT</pubDate></item><item><title><![CDATA[Reply to Pi-Berechnung durch Zufallspfeilwürfe on Sun, 26 Mar 2006 20:42:20 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Vielen Dank für den Tipp!<br />
Aber warum reicht denn nicht, die Zahl durch 10^Ziffernanzahl zu teilen? Es ist doch trotzdem immer ne random zahl? Durch den Ausdruck /Rand-max wurde es lediglich noch etwas komplizierter gemacht, aber vom Prinzip ist das doch nicht anders, oder?<br />
Nochmals danke<br />
Eginobili20</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024455</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024455</guid><dc:creator><![CDATA[eginobili20]]></dc:creator><pubDate>Sun, 26 Mar 2006 20:42:20 GMT</pubDate></item><item><title><![CDATA[Reply to Pi-Berechnung durch Zufallspfeilwürfe on Mon, 27 Mar 2006 06:13:42 GMT]]></title><description><![CDATA[<p>Weil damit die erhaltene Zahl nicht mehr gleichmäßig im Interval [0,1] verteilt liegt. rand() liefert eine gleichverteilte Zufallszahl, wenn du einzelne Werte unterschiedlich stark gewichtest, verschiebst du diese Verteilung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024527</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Mon, 27 Mar 2006 06:13:42 GMT</pubDate></item></channel></rss>