in reply to Randomly generating histograms

Randal's already answered this, so you probably aren't reading further, but here's my go
my $sum = 100; my $elements = 30; my @array; for (1..$sum) { ++$array[ int rand $elements ]; } $array[$_] ||= 0 for @array;
so you can fiddle with the sum, and the number of elements you want at your leisure.

Jasper

Replies are listed 'Best First'.
Re: Re: Randomly generating histograms
by particle (Vicar) on Mar 25, 2002 at 19:14 UTC
    i'd use map for a one liner to replace for:
    map ++$array[rand $elements], 1..$sum;
    also, you can drop the int, like i have. and you mean
    $array[$_] ||= 0 for 0..$elements-1;
    don't you?

    ~Particle ;̃

      i'd use map for a one liner to replace for:
      map ++$array[rand $elements], 1..$sum;
      I wouldn't. This is clearer, faster, and one character shorter:
      ++$array[rand $elements] for 1..$sum;
      Help stamp out void maps and greps!

      -- Randal L. Schwartz, Perl hacker

        too true, too true! i usually take that position--i learned it here. i guess i just couldn't resist showing off a bit.

        thanks for keeping me honest, merlyn

        ~Particle ;̃