Stem-and-leaf plot/Data generator: Difference between revisions

From Rosetta Code
Content added Content deleted
(No newlines.)
(Should produce a bell curve distribution for each hump, now.)
Line 12: Line 12:
return @a;}
return @a;}


# The greater your pointcount to your width, the more pronounced your "hump" will be.
# The greater your pointcount to your width, the more sparse your result.
sub genhump
sub genhump
{
{
Line 18: Line 18:
my $width = shift;
my $width = shift;
my $pointcount = shift;
my $pointcount = shift;
my $slope = shift;

my $diecount = $slope;
my $range = $width / $diecount;


my @ret;
my @ret;
while($pointcount-- > 0)
push @ret, (int(rand($width)+$offset))
{
while(($pointcount--) > 0);
my $point = $offset;
for(my $roll = 0; $roll < $diecount; ++$roll)
{
$point += rand($range * 10000) / 10000;
}

push @ret, int($point);
}


return @ret;
return @ret;
Line 28: Line 40:
my @list = ();
my @list = ();


push @list, &genhump(0, 10, 30);
push @list, &genhump(8, 3, 5, 3);
push @list, &genhump(15, 10, 30);
push @list, &genhump(12, 3, 5, 6);


my @shuffled = &shuffle(@list);
my @shuffled = &shuffle(@list);

Revision as of 02:49, 14 December 2009

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 sparse your result.

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

my $diecount = $slope; my $range = $width / $diecount;

my @ret; while($pointcount-- > 0) { my $point = $offset; for(my $roll = 0; $roll < $diecount; ++$roll) { $point += rand($range * 10000) / 10000; }

push @ret, int($point); }

return @ret; }

my @list = ();

push @list, &genhump(8, 3, 5, 3); push @list, &genhump(12, 3, 5, 6);

my @shuffled = &shuffle(@list);

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