in reply to Re: Re: Re: Function over all letters in a string
in thread Function over all letters in a string

I found that part of the spec ambiguous and so made up a convenient definition.
You also apparently made a convenient definition of the phrase "random number". :)

Personally, I'd like something that has a chance of producing a long string, but favouring shorter ones. Hence, a simple 1 + int rand 12 won't do. Rather, I'm tempted to use an exponential function:

$_ = 'cat'; s/(.)/$1 x int(12**rand)/ge; print;
Which, for example, prints
caaatttttttt

Replies are listed 'Best First'.
Re: Function over all letters in a string
by Abigail-II (Bishop) on Dec 01, 2003 at 11:21 UTC
    But that still puts an arbitrary (low) limit on the number of repetitions. The code below doesn't:
    #!/usr/bin/perl use strict; use warnings; $_ = 'cat'; s/(.)/my $t = $1; $t .= $1 while 0.95 > rand; $t/ge; print "$_\n"; __END__ cccccccccccccaaaaat

    Abigail

Re: Re: Re: Re: Re: Function over all letters in a string
by tilly (Archbishop) on Dec 01, 2003 at 14:39 UTC
    You would have a larger range of possible numbers with:
    $string =~ s=(\w)= $1 x (1 - 1/log(rand)) =eg;