C :: Aufgabe #73

3 Lösungen Lösungen öffentlich

Berechnen von Pi mit Zufallszahlen

Anfänger - C von eulerscheZhl - 07.03.2015 um 07:58 Uhr
Man kann die Kreiszahl Pi näherungsweise bestimmen, indem man Zufallszahlen generiert:
Man stelle sich einen Kreis mit Mittelpunkt (0/0) und Radius 1 vor. Es werden zufällig Punkte erzeugt, bei denen sowohl x als auch y im Intervall [0;1[ liegen. Dann wird die Entfernung dieser Punkte zum Ursprung ermittelt. Ist diese Entfernung kleiner als 1, so liegt der Punkt innerhalb des Kreises.
Setzt man bei einer ausreichenden Zahl von Zufallspunkten die Zahl der Treffer in das richtigen Verhältnis zur Gesamtzahl der Punkte, so erhält man einen Näherungswert für Pi (Pi = 4 * AnzahlTreffer / AnzahlGesamt).

Schreibe ein Programm, das auf oben beschriebene Weise Pi berechnet.

Lösungen:

vote_ok
von Jordan (210 Punkte) - 10.03.2015 um 15:27 Uhr
Quellcode ausblenden C-Code
int main()
{
	float counter= 0;
	float treffer = 0;

	float x = 0;
	float y = 0;
	float wurzel= 0;

	float PI = 0;

	for(int i =0; i<1000000; i++)
	{
		counter = counter +1;
		x = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
		y = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

		wurzel = sqrt((x*x)+(y*y));
		if(wurzel < 1)
		{
			treffer++;
		}		
	}

	printf("anzahl = %f\nTreffer = %f\n",counter, treffer);
	PI = 4* treffer/counter;
	printf("PI = %f\n",PI);


	getchar();
	return 0;
}
vote_ok
von devnull (8870 Punkte) - 13.03.2015 um 08:21 Uhr
Radius-Berechnung mit double-Werten.
Quellcode ausblenden C-Code
/**************************************
* pimcd.c  PI mit Monte Carlo Methode
*          Berechnung mit "double"
***************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define NRSHOTS 10000000

int main( int argc, char **argv ) {
    uint shots, cc, cs;
    double x, y;

    // read from stdin or use default
    shots = (argc>1)?atoi(argv[1]):NRSHOTS;

    // seeding random number generator
    srand48(time(NULL));

    // shooting loop :)
    for (cc=cs=0; cs < shots; cs++) {
       x = drand48();
       y = drand48();
       if (x*x + y*y < 1.0) cc++;
    }
    printf("PI Monte Carlo : %.8f\n", 4.0*cc/cs);
    printf("PI aus math.h  : %.8f\n", M_PI);
    return 0;
}
vote_ok
von devnull (8870 Punkte) - 13.03.2015 um 08:24 Uhr
Radius-Berechnung mit Integer-Werten.
Quellcode ausblenden C-Code
/**************************************
* pimcu.c  PI mit Monte Carlo Methode
*          Berechnung mit "unsigned int"
***************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define NRSHOTS 10000000

int main( int argc, char **argv ) {
    uint shots, cc, cs;
    uint x, y;

    // read from stdin or use default
    shots = (argc>1)?atoi(argv[1]):NRSHOTS;

    // seeding random number generator
    srand(time(NULL));

    // shooting loop
    for (cc=cs=0; cs < shots; cs++) {
       x = (uint)rand();
       y = (uint)rand();
       if ((ulong)x*x + (ulong)y*y < (ulong)RAND_MAX*RAND_MAX) cc++;
    }
    printf("PI Monte Carlo : %.8f\n", 4.0*cc/cs);
    printf("PI aus math.h  : %.8f\n", M_PI);
    return 0;
}
2092639

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.