Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl # This is a quick program to calculate pi using the Monte Carlo method +. # I recomend inputting a value for $cycles greater that 1000. # I am working on a detailed explanation of how and why this works. # I will add it as soon as I'm done. use strict; open(PI, ">>pi.dat") || die "pi.dat"; my ($i, $j, $yespi, $pi) = 1; my $cycles = 1; srand; while ($j <= 100000) { $cycles = $j; while ($i <= $cycles) { my ($x, $y, $cdnt) = 1; $x = rand; $y = rand; $cdnt = $x**2 + $y**2; if ($cdnt <= 1) { ++$yespi; } $i=$i + 10; $pi = ($yespi / ($cycles / 10)) * 4; # since I add 10 every time. print PI "$cycles $pi\n"; } $j = $j + 10; } close(PI) || die "pi.dat";
Here is the modified script I was talking about. If you look at pi.dat it starts getting inacurate at one point. I will try using one of the ways to genereate seeds that are slightly more random than plain srand. Note: you need to make a file pi.dat for the script to run.

In reply to Re: Accuracy of Random Pi by Fingo
in thread Pi calculator by Fingo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2021-06-16 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (74 votes). Check out past polls.

    Notices?