Stem-and-leaf plot/Data generator

From Rosetta Code
Revision as of 02:18, 14 December 2009 by MikeMol (talk | contribs) (No newlines.)

Written to generate data sets for the Stem-and-leaf plot task.

<lang perl>#!/usr/bin/perl use strict;

  1. The shuffle is taken from http://rosettacode.org/wiki/Knuth_shuffle

sub shuffle

{my @a = @_;
 foreach my $n (1 .. $#a)
    {my $k = int rand $n + 1;
     $k == $n or @a[$k, $n] = @a[$n, $k];}
 return @a;}
  1. The greater your pointcount to your width, the more pronounced your "hump" will be.

sub genhump { my $offset = shift; my $width = shift; my $pointcount = shift;

my @ret; push @ret, (int(rand($width)+$offset)) while(($pointcount--) > 0);

return @ret; }

my @list = ();

push @list, &genhump(0, 10, 30); push @list, &genhump(15, 10, 30);

my @shuffled = &shuffle(@list);

print "$_ " foreach ( @shuffled ); </lang>