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

From Rosetta Code
Content added Content deleted
(Data generator)
 
(No newlines.)
Line 33: Line 33:
my @shuffled = &shuffle(@list);
my @shuffled = &shuffle(@list);


print "$_ "
my $idx = 0;
foreach ( @shuffled )
foreach ( @shuffled );
{
if((++$idx % 10) != 0)
{
print "$_\t";
}
else
{
print "$_\n";
}
}
</lang>
</lang>

Revision as of 02:18, 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 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>