[sf-perl] Thinking about psuedo-randomness

Joe Brenner doom at kzsu.stanford.edu
Mon Mar 30 01:56:55 PDT 2009


Randal L. Schwartz <merlyn at stonehenge.com> wrote:

> >>>>> "Joe" == Joe Brenner <doom at kzsu.stanford.edu> writes:
>
> Joe> Josh Berkus <josh at agliodbs.com> wrote:
>
> >> I've been thinking a little about the pseudo-randomness issue some, and
> >> I think there *is* a generalizable case.  However, can you send me your
> >> slides/code so that I can build on what you have rather than starting
> >> from scratch?
>
> Joe> Here's the slides:
> Joe>   http://obsidianrook.com/devnotes/talks/esthetic_randomness/
>
> the code in
>       $tweak = $downers/$uppers;
>       $uppity = int( rand(1 + $tweak) );
>
> has a biased bias.  I think what you wanted is:
>
> $uppity = rand($uppers + $downers) > $uppers;

That's a good one (no need to worry about division by zero), but in
practice there's little difference in the two approaches.  Both have a
slight bias toward lower case (largely because of the way I initialize
the first character):

   scramble_case:
   up: 257970  down: 267406   % bias: -1.796
   scramble_case_exp:
   up: 258261  down: 267115   % bias: -1.685


That's measured like so:

#!/usr/bin/perl
# running_scramble_case                    doom at kzsu.stanford.edu
#                                          29 Mar 2009

use warnings;
use strict;
$|=1;
use Data::Dumper;

our $VERSION = 0.01;

use Getopt::Std;
my %opt = ();
getopts('d', \%opt);
my $DEBUG   = $opt{d} || 0;

use Text::Capitalize qw( scramble_case scramble_case_exp );

my $file = shift || '/home/doom/End/Dust/Texts/RafaelSabatini/1915-the_sea_hawk-seahk10.txt';

print_stats_for( 'scramble_case',     $file );
print_stats_for( 'scramble_case_exp', $file );

sub print_stats_for {
  my $routine = shift;
  my $file    = shift;
  open my $fh, '<', $file or die "$!";

  print "$routine:\n";
  my ($total_up, $total_down, $bias);
  while( my $line = <$fh> ) {
    my $transformed;
    { no strict 'refs';
        $transformed = $routine->(  $line );
    }
    print "$transformed\n" if $DEBUG;
    my $copy_1 = $transformed;
    my $copy_2 = $transformed;
    my $uppers = ( $copy_1 =~ s{ [[:upper:]] }{}xmsg );
    my $lowers = ( $copy_2 =~ s{ [[:lower:]] }{}xmsg );
    $total_up   += $uppers;
    $total_down += $lowers;
  }
  $bias = ( ($total_up - $total_down) / ($total_up + $total_down) ) * 100;
  printf "up: %4d down: %4d  %% bias: %2.3f\n", $total_up, $total_down, $bias;
}



More information about the SanFrancisco-pm mailing list