Buchempfehlung
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
"Der" Petzold, das über 1000 Seiten starke Standardwerk zum Win32-API - besonders nützlich u. a. bei der GUI-Programmierung in FreeBASIC! [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Code-Beispiel

Code-Beispiele » Mathematik

Buffonsches Nadelproblem

Lizenz:Erster Autor:Letzte Bearbeitung:
k. A.MitgliedArhuerl 15.09.2014
'Das Buffonsche Nadelproblem fragt nach der Wahrscheinlichkeit,
'dass eine willkürlich geworfene Nadel ein Gitter paralleler Linien schneidet.
'Es erlaubt unter anderem, die Kreiszahl Pi experimentell zu bestimmen.

'Initialize
Screen (20)
'draw two lines
Line (50, 200)-(950, 200), 15
Line (50,500)-(950,500), 15

'Initialize variables
Dim theta As Double
Dim yoffset As Double
Dim matx As Double
Dim maty As Double
Dim hit As Integer
Dim x As Integer
Dim zahlpi As Double
Dim calcpi As Double
Dim col As Integer
Dim vorzeichen As Integer

'Initialize random numbers
Randomize

zahlpi =3.141592654

'Repeat "Throwing" the needles until x reaches the number of repetitions
While x<1000
If Rnd > 0.5 Then
    vorzeichen=-1
Else vorzeichen=1
EndIf
'random position
yoffset = Int (Rnd * 149 *vorzeichen)
'random angle
theta =Rnd * 2 *zahlPi
    maty=Int(150*Sin(theta))
    matx=Int(150*Cos(theta))
    col=col+1
    If col>13 Then col=1
    'Draw colorized "needle"
    Line ((500-matx),(350+yoffset-maty))-((500+matx),(350+yoffset+maty)),col
'findout if "needle" is crossing lines
    If (350+yoffset-Abs(maty))<0200 Then hit=hit+1
    If (350+yoffset+Abs(maty))>=500 Then hit=hit+1

x=x+1
Wend
'calculate Pi
calcpi=2*(x/hit)
'Print results
Print "throws: "; x
Print "hits: "; hit
Print "PI calculated: "; calcpi
Print "difference to pi:"; (zahlpi-calcpi)
sleep

Zusätzliche Informationen und Funktionen
  • Das Code-Beispiel wurde am 15.09.2014 von MitgliedArhuerl angelegt.
  • Die aktuellste Version wurde am 15.09.2014 von MitgliedArhuerl gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen