in reply to A little golfing challenge: replace digits by random letters

rand knows all!

echo 123412341234 | perl -pe '$s=time;s/(.)/srand $s;rand for 1..$1;ch +r(ord("a")+rand 26)/ge'

bw, bliako

Edit: fixed the 'a' to "a" also a warning: this code should not be run more than once during one whole second, thanks Discipulus

Edit: Of course that warning is because of srand(time). Now this seeds and on something completely different: echo '01234567890123456789' | perl -pe '$s=time+join "",{}=~/(\d+)/g;s/(.)/srand $s;rand for 0..$1;chr(ord("a")+rand 26)/ge'. (👁️)

Replies are listed 'Best First'.
Re^2: A little golfing challenge: replace digits by random letters
by Discipulus (Canon) on Feb 10, 2019 at 17:45 UTC
    Hello bliako,

    apart the typo ( 'a' insted of "a") I see duplicates in you code output and more characters than expected

    See it here

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Hello there Discipulus, grazie for the alerts and for fixing the quotes issue serving me my own medicine.

      The duplicates are because you ran it too fast! You need to run this code once a second or slower. The universe needs some time to settle ... i.e. the RNG seeds on time whose granularity is the second.

      However, here is a version which seeds on something different:

      echo '01234567890123456789' | perl -pe '$s=time+join "",{}=~/(\d+)/g;s +/(.)/srand $s;rand for 0..$1;chr(ord("a")+rand 26)/ge'

      More characters than expected I have not observed.

      This test may be useful for others too:

      echo '01234567890123456789' | perl -MTest::More -pe ' my $inp = $_; my $L = length($inp); my $N = 100; # number of trials $nt=0; my %previous = (); for(1..$N){ my $tmp = $inp; my $s = join "$_", {} =~ /(\d+)/g; # because ... $tmp =~ s/(.)/srand $s;rand for 0..$1;chr(ord("a")+rand 26)/ge +; $previous{$tmp} = 1; ok(length($tmp)==$L, "length of conversion (".length($tmp).") +equals expected ($L)."); $nt++; } ok(scalar(keys %previous)==$N, ($N-scalar(keys %previous))." duplicate +s among $N trials"); $nt++; done_testing($nt); '