in reply to Re: PRNG/TRNG Cesaro's theorem
in thread PRNG/TRNG Cesaro's theorem

Yes I meant TRNG, sorry was typing fast. and yes It is Stolz-Cesaro theorem. I understand the theorem but I have to write two differnt prng's that have pairs of at least 1000 numbers and estimate pie through the theorem. I dont know how to write it so it generates the numbers in pairs however... I also have to use random.org for the TRNG.

Replies are listed 'Best First'.
Re^3: PRNG/TRNG Cesaro's theorem
by AnomalousMonk (Archbishop) on Oct 08, 2017 at 03:25 UTC
    ... not sure what a TPRNG is ... I meant TRNG ...

    You can correct your OP. Please see How do I change/delete my post? for site etiquette and protocol regarding changing a post.


    Give a man a fish:  <%-{-{-{-<

Re^3: PRNG/TRNG Cesaro's theorem
by Anonymous Monk on Oct 08, 2017 at 01:24 UTC
    Well, the Stolz-Cesaro theorem is about convergence of series. There are a lot of series that converge to pi, did you have a specific one in mind? And I don't see where random numbers come into the picture at all.

      Update 1: Added example using Inline::C.

      Update 2: Do more work, ... for my $e ( 1..8 ) { ... }

      Well, the Stolz-Cesaro theorem is about convergence of series. There are a lot of series that converge to pi, did you have a specific one in mind? And I don't see where random numbers come into the picture at all.

      Some time ago, a University Professor emailed me a serial implementation and asked if possible to run parallel. The number sequence generator in MCE makes it seamless. Also, enabling the bounds_only option further decreases latency by providing workers just the begin and end values per each chunk/segment.

      Perl

      use strict; use warnings; use MCE::Flow; my $N; # Workers receive [ begin, end ] values. MCE::Flow::init( max_workers => MCE::Util::get_ncpu(), chunk_size => 100000, bounds_only => 1, user_begin => sub { $N = MCE->user_args()->[0] } ); sub func { my ( $beg_seq, $end_seq ) = @{ $_ }; my ( $pi, $t ) = ( 0.0 ); for my $i ( $beg_seq .. $end_seq ) { $t = ( $i + 0.5 ) / $N; $pi += 4.0 / ( 1.0 + $t * $t ); } MCE->gather($pi); } # The user_args option is how to pass arguments. # Workers persist between each run. for my $e ( 1..8 ) { my $n = 10 ** $e; my @ret = mce_flow_s { user_args => [$n] }, \&func, 0, $n - 1; my $pi = 0.0; $pi += $_ for @ret; printf "%9d %0.14f\n", $n, $pi / $n; }

      Output

      10 3.14242598500110 100 3.14160098692313 1000 3.14159273692312 10000 3.14159265442313 100000 3.14159265359816 1000000 3.14159265358988 10000000 3.14159265358979 100000000 3.14159265358979

      Inline::C

      use strict; use warnings; use Inline 'C' => <<'END_C'; unsigned int N = 0; void c_init( unsigned int n ) { N = n; } double c_func( unsigned int beg_seq, unsigned int end_seq ) { double t, pi = 0.0; unsigned int i; for ( i = beg_seq ; i <= end_seq ; i++ ) { t = (double) i / (double) N; pi += 4.0 / ( 1.0 + t * t ); } return pi; } END_C use MCE::Flow; # Workers receive [ begin, end ] values. MCE::Flow::init( max_workers => MCE::Util::get_ncpu(), chunk_size => 100000, bounds_only => 1, user_begin => sub { c_init( MCE->user_args()->[0] ) } ); sub func { my ( $beg_seq, $end_seq ) = @{ $_ }; my $pi = c_func($beg_seq, $end_seq); MCE->gather($pi); } # The user_args option is how to pass arguments. # Workers persist between each run. for my $e ( 1..8 ) { my $n = 10 ** $e; my @ret = mce_flow_s { user_args => [$n] }, \&func, 0, $n - 1; my $pi = 0.0; $pi += $_ for @ret; printf "%9d %0.14f\n", $n, $pi / $n; }

      Output

      10 3.23992598890716 100 3.15157598692313 1000 3.14259248692312 10000 3.14169265192314 100000 3.14160265357315 1000000 3.14159365358964 10000000 3.14159275358979 100000000 3.14159266358980

      The C code is greater than 5 times faster on the Windows platform (32-bit), beyond 15 times faster on Unix platforms (64-bit).

      Regards, Mario

        That's great, but it runs in two seconds single threaded. A tenth of a second in C.
          A reply falls below the community's threshold of quality. You may see it by logging in.
      basically i have to write a code that generates a list of number pairs using the formula provided to estimate pie the theorm being what i posted a few min ago . (Cesaro's theorem states that given two random integers, x and y, the probability that gcd(x, y) = 1 is 6/(Pi^2).) and I to mark down the number pairs that are equate to one I believe from what my teacher said and the project states.
Re^3: PRNG/TRNG Cesaro's theorem
by RonW (Parson) on Oct 09, 2017 at 22:50 UTC
    I dont know how to write it so it generates the numbers in pairs however.

    In your loop, call the RNG twice:

    for (1 .. 500) { my $x = rng(); my $y = rng(); ... }