Once again, is seek enlightenment. I have the code (below) that works (with 1 mod), but I can't explain why. I have an interest in random numbers. So I tried the iterator code from Higher Order Perl to create an independent RNG as an iterators.

The code contains page references to HOP as to where the individual parts came from that I pulled together. For 5 hours, I couldn't get a successful run. The commented line labeled "!!! Important ..." was missing.

It was way back in the iterator chapter, and simply called "a bit of sugar". There is no explanation of that sort of magic it is causing the Perl interpreter to do. It seems to be a duplicate definition for the function 'Iterator' .

Can someone explain what this 'sugar' does and how?

use strict; # Definition from HOP page 122 sub NEXTVAL { $_[0]->() } # HOP top of page 123, called "a bit of sugar" ??? #sub Iterator (&) { return $_[0] } #!!Important to make this work unc +omment!!! #HOP page 156 sub make_rand { my $seed = shift || (time & 0x7ffff); print "Seed=$seed\n"; return Iterator { #print " << $seed\n"; $seed = (13 * $seed + 31011) & 0x7ffffff; #print " >> $seed\n"; return $seed; }; } # My test code my $rng = make_rand(); # a reference to an "Iterator" code different # from the one defined originally ?? my @bucket; my $i; my $t; #printf " ==%d\n",(2**31); # HOP pages 156,7 for $i (1..10000){ my $rndx = NEXTVAL ($rng); #print " $rndx "; $rndx %= 100; #print " $rndx\n"; $bucket[$rndx] ++; } # test code $t=0; for $i (0..99) { print "$bucket[$i]- "; $t+=$bucket[$i]; } printf "\nAvg= %6.2f\n", $t/100; __END__
Aside: While very "advanced", I would probably *not* use this technique due to the corresponding very high "obscure" and "unmaintainable" score.

It is always better to have seen your target for yourself, rather than depend upon someone else's description.


In reply to "a bit of sugar "(HOP) by Wiggins

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.