Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: A little golfing challenge: replace digits by random letters

by bliako (Monsignor)
on Feb 10, 2019 at 19:44 UTC ( [id://1229737]=note: print w/replies, xml ) Need Help??


in reply to Re^2: A little golfing challenge: replace digits by random letters
in thread A little golfing challenge: replace digits by random letters

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); '

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229737]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-16 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found