Ever wanted to do some random mathematical operations on some numbers? Well, me neither, but here's some perl code that'll let you do it anyway. The function takes two arguments: both optional. It's self explanatory, pretty much, and is customizable via variables. Here goes; critique if you wish:

#of coure, use strict && use warnings wherever you put this code sub math_stuff { my @bin = qw(+ - * / % << >> & | ^); my $un = qw(~); my $max = 2_041; my $done = 20; my $left = $_[0] || int(rand(int($max/2))); my $right = $_[1] || int(rand($max-$left)); my $rv = eval "int($left $bin[int(rand($#bin))] $right) & $max"; do { my $oper_b = $bin[int(rand($#bin))]; my $oper_u = int(rand(50)) > 25 ? $un : " "; $oper_u = " " if ($done - $_) <= 5; my $num = int(rand(50)) > 25 ? $left : $right; if($oper_b eq "%" || $oper_b eq "<<" || $oper_b eq ">>") { unless((eval "$oper_u $num") > $rv) { $num ^= $rv; $rv ^= $num; $num ^= $rv; } } $rv = eval "int($oper_u $num $oper_b $rv) & $max"; } for 1..$done; $rv ^ $max & int(rand(int($max/5))); }

In reply to Let's try some math! by dimmesdale

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.