C# :: Aufgabe #258

7 Lösungen Lösungen öffentlich

Stochastische Ermittlung der Eulerschen-Zahl

Anfänger - C# von hollst - 12.04.2019 um 09:37 Uhr
Wir betrachten folgendes Zufallsexperiment: Aus einer Menge reeller und gleichverteilter Zahlen {X} mit 0 <= x < 1.0
ziehen wir solange Zahlen x1, x2 ... xn bis deren Summe >= 1.0 ist. n ist dann das Ergebnis eines Zufallsexperimentes.
Wir müssen mindestens zweimal ziehen, den ein einzelnes Zufalls-x ist ja immer kleiner als 1.0.

Die Frage lautet: Wie groß ist n im Mittel?

Hinweis: Laut Theorie ist n = 2.718281828459045... (= e). Nun gut, grau ist alle Theorie, wir wollen sehen, ob der Computer
(in etwa) der gleichen Meinung ist.

Viel Spaß!

Lösungen:

vote_ok
von Z3RP (1020 Punkte) - 23.04.2019 um 13:48 Uhr
Quellcode ausblenden C#-Code
static void stochastischeErmittlung()
		{
			double bigSum = 0;
			int i = 1;
			while (true)
			{
				double sum = 0;
				int iterations = 0;
				Random rnd = new Random();
				while (sum < 1)
				{
					sum += rnd.NextDouble()* 0.9;
					iterations++;
				}
				bigSum += iterations;
				i++;

				Console.Clear();
				Console.WriteLine(i+": "+bigSum / i);
				Thread.Sleep(16);
			}
		}
vote_ok
von hollst (13980 Punkte) - 17.05.2019 um 12:43 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;

namespace aufgabe_258
{
    class Program
    {
        static void Main()
        {
            Random random = new Random();
            UInt32 nmax = (UInt32)1E+9;
            UInt32 glob_counter = 0;
            for (UInt32 n = 0; n < nmax; n++)
            {
                Double temp = 0.0;
                UInt32 counter = 0;
                while(temp < 1.0)
                {
                    temp += random.NextDouble();
                    counter++;
                };
                glob_counter += counter;
            }
            WriteLine($"exact:  {Math.E}");
            WriteLine($"approx: {1.0 * glob_counter / nmax}");
            ReadKey();
        }
    }
}
vote_ok
von t.wi (660 Punkte) - 12.06.2019 um 12:21 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace übung258
{
    class Program
    {
        static void Main(string[] args)
        {
            double countersum = 0;
            Random zufall = new Random();
            for(int c = 1; c <=1000;c++)
            {
                double xsum = 0;
                int counter = 0;
                while (xsum < 1)
                {
                    double x = zufall.Next(0, 10);
                    x = x / 10;
                    Console.WriteLine(x);
                    xsum = xsum + x;
                    counter++;
                }
                Console.WriteLine("Finale Summe: " + xsum);
                Console.WriteLine("Anzahl zahlen: " + counter+"\n");
                countersum = countersum + counter;
            }
            Console.WriteLine("Durschnitt der Benötigten zahlen: "+countersum / 1000);
            Console.ReadKey();
        }
    }
}
vote_ok
von suppengruen (200 Punkte) - 12.06.2019 um 16:16 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;

namespace EulerscheZahl
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> n = new List<int>();
            Random rand = new Random();
            int count = 0, mid = 0;
            double x = 0;
            float mitte = 0.0f, anzahl;
            Console.WriteLine("Gib an wie viele Schleifendurchläufe du machen willst...");
            anzahl = float.Parse(Console.ReadLine()); 
            for(int y = 0; y < anzahl; y++)
            {
                do
                {
                    x = x + rand.NextDouble();
                    count++;
                } while (x < 1.0);
                n.Add(count);
                count = 0;
                x = 0;
            }
            
            n.ForEach(delegate (int zahl)
            {
                mid = mid + zahl;
            });
            mitte = mid / anzahl;
            Console.WriteLine("Das Ergebnis ist: {0}",mitte);
            Console.ReadLine();
        }
    }
}
vote_ok
von Kotgreifer (1100 Punkte) - 18.06.2019 um 10:41 Uhr
Quellcode ausblenden C#-Code
using System;
namespace Euler
{
    class Program
    {
        static void Main(string[] args)
        {
            int glb_counter = 0;
            Random rnd = new Random();
            for (int i = 0; i < 1000000; i++)
            {
                double tmp = 0.0;
                int counter = 0;
                while (tmp<1)
                {
                    tmp += rnd.NextDouble();
                    counter++;
                }
                glb_counter += counter;
            }
            double erg = (double)glb_counter / 1000000;
            Console.WriteLine(erg);
            Console.Read()
        }
    }
}

vote_ok
von Waldgeist (2310 Punkte) - 04.11.2019 um 19:12 Uhr
Ich habe es als Windows-Form App geschrieben:

Form1.cs

Quellcode ausblenden C#-Code
using System;
using System.Windows.Forms;

namespace Aufgabe_258
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double Wiederholung = Convert.ToDouble(Wiederholungen.Text);
            int zaehler = 1;


            Random Zufall = new Random();

            for (int i = 1; i <= Wiederholung; i++)
            {
                double summe = 0;
                do
                {

                    double x = Zufall.NextDouble();
                    summe = summe + x;
                    zaehler++;
                } while (summe <= 1.0);


            }
            Funktionswert.Text = Convert.ToString(Math.E);
            Ausgabe.Text = Convert.ToString(zaehler / Wiederholung);
            Differenz.Text = Convert.ToString(Math.E - zaehler / Wiederholung);
        }

    }
}




Form1.Designer.cs

Quellcode ausblenden C#-Code
namespace Aufgabe_258
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.Wiederholungen = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.Ausgabe = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.Funktionswert = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.Differenz = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(68, 100);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(85, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Wiederholungen";
            // 
            // Wiederholungen
            // 
            this.Wiederholungen.Location = new System.Drawing.Point(179, 93);
            this.Wiederholungen.Name = "Wiederholungen";
            this.Wiederholungen.Size = new System.Drawing.Size(182, 20);
            this.Wiederholungen.TabIndex = 1;
            this.Wiederholungen.Text = "10";
            this.Wiederholungen.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(179, 150);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(182, 43);
            this.button1.TabIndex = 2;
            this.button1.Text = "Berechnen";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(630, 388);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(156, 51);
            this.button2.TabIndex = 3;
            this.button2.Text = "Beenden";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(62, 269);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(91, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Berechneter Wert";
            // 
            // Ausgabe
            // 
            this.Ausgabe.Location = new System.Drawing.Point(178, 262);
            this.Ausgabe.Name = "Ausgabe";
            this.Ausgabe.ReadOnly = true;
            this.Ausgabe.Size = new System.Drawing.Size(183, 20);
            this.Ausgabe.TabIndex = 5;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(55, 329);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(98, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Theoretischer Wert";
            // 
            // Funktionswert
            // 
            this.Funktionswert.Location = new System.Drawing.Point(178, 322);
            this.Funktionswert.Name = "Funktionswert";
            this.Funktionswert.ReadOnly = true;
            this.Funktionswert.Size = new System.Drawing.Size(183, 20);
            this.Funktionswert.TabIndex = 7;
            this.Funktionswert.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(104, 371);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(49, 13);
            this.label4.TabIndex = 8;
            this.label4.Text = "Differenz";
            // 
            // Differenz
            // 
            this.Differenz.Location = new System.Drawing.Point(179, 364);
            this.Differenz.Name = "Differenz";
            this.Differenz.ReadOnly = true;
            this.Differenz.Size = new System.Drawing.Size(182, 20);
            this.Differenz.TabIndex = 9;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.Differenz);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.Funktionswert);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.Ausgabe);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.Wiederholungen);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Aufgabe 258 / Ermittlung der Eulerschen Zahl";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox Wiederholungen;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox Ausgabe;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox Funktionswert;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox Differenz;
    }
}


vote_ok
von luckyman81 (550 Punkte) - 13.04.2020 um 21:42 Uhr
Quellcode ausblenden C#-Code
using System;

namespace CS_Aufgabe_258_Eulerzahl
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            double rndNumber, sum = 0.0, sumN = 0;
            int n = 0;

            for (int i = 0; i < 1e8; i++)
            {

                while (true)
                {
                    rndNumber = rnd.NextDouble();
                    sum += rndNumber;
                    n++;
                    if (sum > 1.0) break;
                }
                sumN += n;
                n = 0;
                sum = 0.0;
            }
            Console.WriteLine($"Abweichung = {sumN / 1e8 - Math.E}");
            Console.ReadKey();
        }
    }
}