in reply to Accuracy of Random Pi
in thread Pi calculator
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.#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Srand versus rand
by gryng (Hermit) on Feb 16, 2001 at 11:21 UTC | |
by extremely (Priest) on Feb 16, 2001 at 12:39 UTC | |
by Fingo (Monk) on Feb 16, 2001 at 16:12 UTC | |
by gryng (Hermit) on Feb 16, 2001 at 18:26 UTC | |
by tilly (Archbishop) on Feb 16, 2001 at 18:12 UTC | |
by gryng (Hermit) on Feb 16, 2001 at 18:35 UTC | |
by tilly (Archbishop) on Feb 16, 2001 at 19:28 UTC | |
by gryng (Hermit) on Feb 16, 2001 at 20:19 UTC | |
by Fingo (Monk) on Feb 17, 2001 at 01:03 UTC | |
by gryng (Hermit) on Feb 19, 2001 at 19:15 UTC |