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)));
}