in reply to Finding a random position within a long string (Activeperl Build 822)

Depending on what you mean by "somehow equally distributed over the string", this may do the trick
my $percent = 10; my $string = 'A' x 20; for ( 1 .. $percent / 100 * length $string ) { my $pos = int rand length $string; substr( $string, $pos, 1 ) = '?'; }

Update: int isn't needed

Replies are listed 'Best First'.
Re^2: Finding a random position within a long string (Activeperl Build 822)
by mwah (Hermit) on Oct 12, 2007 at 21:47 UTC
    FunkyMonkthis may do the trick

    try:
    my $percent = 10; my $string = 'A' x 1_000_000; for ( 1 .. $percent / 100 * length $string ) { my $pos = int rand length $string; substr( $string, $pos, 1 ) = '?'; } print $string =~ y/?//;
    Sth. like this is the approach I initially took (and failed).

    If you check that out (with a larger string), youll see the print-
    out from the last line will approach 0xffff.

    That means (I'm struggling to say this)
       In Activeperl/Win, the RAND_MAX of 
       the underlying clib is promoted 
       into perl?
    
    Is this documented?

    Regards & Thanks

    mwa
Re^2: Finding a random position within a long string (Activeperl Build 822)
by kyle (Abbot) on Oct 12, 2007 at 21:39 UTC

    This might get a little fewer than the percentage requested because it's possible for it to pick the same position more than once. Try setting $percent = 90 for a good demonstration. I ran it ten times, and it never replaced more than 13/20 (well short of the expected 18/20).

      I took "about ~10%" to mean about 10% ;)