Hmmmm, is Obfu the right place for golf? I hope so.

My wife is tinkering with the intersection of my world (machines) with her world (biology), and made the mistake of wondering aloud if a program could be written to generate random gene sequences (GTAC, etc.)

Why? Beats me. But it sure sounded like golf. So, without further ado:

Write a program to produce a string of any given length of a random sequence of the letters G, T, A, and C.

I came up with a one-liner in 41:

perl -e 'for(1..shift){print qw(G T A C)[rand(4)]}' 1000

Humblest apologies in advance if this is too easy, too dull, too hard, been done, etc.

Peace,
-McD

Replies are listed 'Best First'.
Re: MiniBioGolf
by jmcnamara (Monsignor) on Feb 21, 2002 at 00:25 UTC

    Here are some variations on a theme. The last shaves 1 from tilly's.
    #23456789_123456789_123456789_123456789_1234567 (47) perl -e'split$,,GATC;print@_[rand 4]for+1..pop' 1000 #23456789_123456789_123456789_123456789_12345 (45) perl -e'print+substr+GATC,4*rand,1for+1..pop' 1000 #23456789_123456789_123456789_123456789_12 (42) perl -e'print+(G,T,A,C)[rand 4]for+1..pop' 1000

    --
    John.

Re: MiniBioGolf
by blakem (Monsignor) on Feb 20, 2002 at 22:51 UTC
    Here is a slightly shorter version....
    #23456789_123456789_123456789_123456789_123456 perl -e'print map qw(G T A C)[rand(4)],1..pop' 1000
    And this trims one more char, but is only valid in 5.6 and above....
    #23456789_123456789_123456789_123456789_12345 perl -e'print qw(G T A C)[rand(4)]for 1..pop' 1000

    Update: hmm, looks like all solutions so far are only valid in 5.6 and above. Below 5.6 you'd have to add a set of parens around your list...

    $c = (qw(A B C))[2];

    -Blake

      Shaving and making it legal in earlier Perls:
      #23456789_123456789_123456789_123456789_123 perl -e'print((G,T,A,C)[rand 4])for 1..pop' 1000
Re: MiniBioGolf
by giulienk (Curate) on Feb 21, 2002 at 14:34 UTC
    Shave one more char, but dies.
    #23456789_123456789_123456789_123456789_1 perl -e"die map{(G,T,A,C)[rand 4]}1..pop" 1000

    $|=$_='1g2i1u1l2i4e2n0k',map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g