In what ways can you scramble the English alphabet so that it can still be sung to the tune Twinkle Twinkle Little Star without ruining the tune?



Let's just assume the letters contained in each of the following sets can be swapped freely by default without ruining the tune:

  • { A, J, K }
  • { B, C, D, E, G, P, T, V, Z }
  • { I, Y }
  • { Q, U }
  • { S, X, F }
  • { M, N }
  • Therefore H, L, O, R, and W are locked in place


The program needs to output a single RANDOM string (or list of characters) containing the complete English alphabet in any order provided that order satisfies the conditions above. There should be no way for you to predict which string your program will output(if we ignore seeding), meaning you can't just hardcode it.

Your program must have some positive probability (not necessarily uniform) of generating each of the \$ 9! \cdot 3! \cdot 3! \cdot 2! \cdot 2! \cdot 2! = 104509440 \$ outputs.

There are no particular formatting restrictions regarding spacing, delimiters or case, just be consistent.


Fewest bytes wins!


  • A,G,Z,V,P,X,C,H,Y,K,J,L,N,M,O,T,U,R,S,D,Q,B,W,F,I,E
  • j c d e b x t h i k a l n m o g u r s v q p w f y z



Proof of Concept: (Python3, 529 bytes)

import random
g1 = ['A', 'J', 'K']
g2 = ['B', 'C', 'D', 'E', 'G', 'P', 'T', 'V', 'Z']
g3 = ['I', 'Y']
g4 = ['Q', 'U']
g5 = ['S', 'X', 'F']
g6 = ['M', 'N']
print(g1[0] + g2[0] + g2[1] + g2[2] + g2[3] + g5[0] + g2[4] + 'H' + g3[0] + g1[1] + g1[2] + 'L' + g6[0] + g6[1] + 'O' + g2[5] + g4[0] + 'R' + g5[1] + g2[6] + g4[1] + g2[7] + 'W' + g5[2] + g3[1] + g2[8])
  • 5
    \$\begingroup\$ Shouldn't Z be "locked in place", it doesn't rhyme with the others? \$\endgroup\$ – Shaggy Jul 10 '19 at 7:37
  • 3
    \$\begingroup\$ Depends on where you are from I suppose. If you say 'zed' then it would make sense to take it out but otherwise, if you say 'zee' then leave it in. Ultimately it's up to you, as are the rest of the sets. They're supposed to be guidelines and starting points opposed to strict rules :) \$\endgroup\$ – breadlord Jul 10 '19 at 7:46
  • 3
    \$\begingroup\$ Uniformly random or every possibility having a non-zero probability, or something else? \$\endgroup\$ – jimmy23013 Jul 10 '19 at 9:32
  • 8
    \$\begingroup\$ @PeterTaylor I think the intention is that the members of the groups can be easily swapped in the middle of the song while maintaining the tune and rhythm of the original - so while they do rhyme, W is 3 syllables long while U is only 1, which would change the rhythm of the song. \$\endgroup\$ – Sok Jul 10 '19 at 10:18
  • 2
    \$\begingroup\$ For those who (like me) had no idea what the question was talking about: en.wikipedia.org/wiki/Alphabet_song \$\endgroup\$ – anatolyg Jul 11 '19 at 10:36

14 Answers 14


05AB1E, 28 bytes


Outputs as a single lowercase string.

Try it online or verify \$n\$ random outputs at once.


A                    # (a) Push the lowercase alphabet
 .•¬=©ƶÓÄûkTVã”ØζÞ•  # Push compressed string "ajk bcdegptvz iy qu sxf mn"
  Dá                 # (b) Duplicate it, and only keep the letters (removing the spaces)
    s#               # Swap to get the string again, and split it by spaces
      €.r            # Shuffle each substring randomly
         J           # (c) Join it back together to a single string
          ‡          # Transliterate all characters from (b) to (c) in string (a)
                     # (and output the result implicitly)

See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•¬=©ƶÓÄûkTVã”ØζÞ• is "ajk bcdegptvz iy qu sxf mn".

| improve this answer | |

Python 3, 140 133 124 123 bytes

d=*map(set,'AJK BCDEGPTVZ IY QU SXF MN H L O R W'.split()),
print([d[int(c,16)].pop()for c in'0111141620075581394131a421'])

Try it online!

-1 byte, thanks to Jo King

Python 2, 174 170 158 bytes

from random import*
for c in'abbbbebHcaaLffObdRebdbWecb':s+=choice(list(set(('AJK BCDEGPTVZ IY QU SXF MN '+c).split()['abcdef'.find(c)])-set(s)))
print s

Try it online!

| improve this answer | |

Ruby, 102 bytes

%w(AJK BCDEGPTVZ IY QU SXF MN).map{|e|a=e.chars.shuffle;s.gsub!(/[#{e}]/){a.pop}}

Try it online!

| improve this answer | |

Pyth, 59 57 56 bytes

hMeD,Vs.SMJc"ajk bcdegptvz iy qu fsx mn h l o r w"dxLGsJ

Try it online!

Output is an array of lower case letters.

hMeD,Vs.SMJc"ajk bcdegptvz iy qu fsx mn h l o r w"dxLGsJ   Implicit: d=" ", G=<lowercase alphabet>
          Jc"ajk bcdegptvz iy qu fsx mn h l o r w"d        Chop the grouping string on spaces, store in J
                                                      sJ   Concatenate J into single string
                                                   xLG     Find the index of each letter in grouping string in the unaltered alphabet
       .SMJ                                                Shuffle each group in J
      s                                                    Concatenate into a single string
    ,V                                                     Pair the shuffled string with their 'correct' positions in the alphabet
  eD                                                       Order the pairs by the derived positions (last element of each pair)
hM                                                         Keep the letter from each pair (the first element)
                                                           Implicit print
| improve this answer | |

R, 93 91 bytes

for(s in el('AJK7BCDEGPTVZ7IY7QU7FSX7MN'^7)^'')L[L%in%s]=sample(s)

Try it online!

| improve this answer | |

Perl 6, 76 bytes

{my@a='A'..'Z';<AJK BCDEGPTVZ IY QU SXF MN>>>.&{@a[.ords X-65].=pick(*)};@a}

Try it online!

Anonymous code block taking no arguments and returning a list of characters.


{                                                                          } # Anonymous code block
 my@a='A'..'Z';      # Initialise @a as a list of the alphabet
               <AJK BCDEGPTVZ IY QU SXF MN>   # For each of the sets of letters
                                           >>.&{@a[.ords X-65].=       }  # Set those indexes
                                                                pick(*)   # To a random order
                                                                        ;@a  # And return
| improve this answer | |

JavaScript - 421 344 328 320 306 280 277 276 ... 176 Bytes

-77 Bytes - on my own

-18 Byte - thanks to @tsh and @Geza Kerecsenyi who made me see what @tsh initially pointed out too

-8 Bytes - thanks to @Geza Kerecsenyi

-14 Bytes - with help of @Geza Kerecsenyi

-28 Bytes - on my own

-3 Bytes - again with with help of @Geza Kerecsenyi

-1 Bytes - how could this happen...


-100 Bytes - @Kaiido killed it and via some steps before this whole thing got down to 176 Bytes



or try it online!

| improve this answer | |
  • 1
    \$\begingroup\$ You could at least replace ['B','C','D','E','G','P','T','V','Z'] by 'BCDEGPTVZ'.split`` to save some bytes \$\endgroup\$ – tsh Jul 10 '19 at 9:32
  • 1
    \$\begingroup\$ Try 'BCDEGPTVZ'.split`` instead of .split('') for -2. \$\endgroup\$ – Geza Kerecsenyi Jul 10 '19 at 10:54
  • 1
    \$\begingroup\$ Also, you can define y=q=>q.split`` at the top of your code, and make all of the arrays strings that you pass into y() - e.g a=['A','J','K'] becomes a=y("AJK") \$\endgroup\$ – Geza Kerecsenyi Jul 10 '19 at 11:02
  • 1
    \$\begingroup\$ And replace 'BCDEGPTVZ'.split('') with y('BCDEGPTVZ') \$\endgroup\$ – Geza Kerecsenyi Jul 10 '19 at 11:08
  • 1
    \$\begingroup\$ 'abcdef'.includes(s)?r(eval(s)):l[i] \$\endgroup\$ – Geza Kerecsenyi Jul 10 '19 at 11:54

Runic Enchantments, 210 bytes

>yy `AJK`06B$̤$@
>`BCDEGPTVZ`06B$$$$ $̤$y $ $y @
>̤`IY`06Byyy$yyy̤ @
> ̤`QU`06Byy̤ $y @
> ̤`FSX`06B $yy̤$yy@
>y̤ `MN`06Byyy $@
> ̤`HLORW`06Bo$y $y$y$yy@ \~{{B͍

Try it online!

The randomization is not uniform as there isn't a good way to do that in Runic. Instead it randomly rotates each collection of letters (e.g. [BCDEGPTVZ] is one grouping) by some amount (e.g. rotating the above set by 4, where the top of the stack is at the right, the result would be [BCDEGZPTV]) and then randomly decides whether or not to reverse the stack. It performs these operations 15 times. As a result, all possible orderings are possible but not equally likely. (In the event that this is not enough, increasing it further costs zero bytes, up to 15000 shuffle loops).

This is the section of the code that handles the shuffling:

  v              vvvv           Loop counter

}}f}l3-[r\                      < Loop entry
                       \~{{B͍    < loop exit

 ^^^^^^                         Randomly reverse
          ^^^^^                 Rotate by a random an amount

The rest of the code unrolls into this:

>      `AJK`08B      $        $$;
> `BCDEGPTVZ`08B      $$$$ $        $   $ $   $;
>         `IY`08B            $               $;
>          `QU`08B                   $   $;
>          `FSX`08B       $            $    $;
>            `MN`08B             $$;
>          `HLORW`08Bo      $   $  $  $    $;

^                                                   Create IPs
 ^^^^^^^^^^^^^^^^                                   Set letter groupings
                  ^^^                               Call shuffle function
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^    Print letters
                     ^                              Last letter group needs to be sorted

If the letters are left unshuffled (but once-reversed) by changing two bytes the alphabet is printed normally, which can be used to verify that all letter groupings print in the correct places. The whitespace shifting the B commands out of phase is so that all the IPs can use the function loop at the same time without colliding, and then getting them back into phase again.

To golf, first any space that could be removed on all lines was trimmed, then each two spaces were converted to a y, and every sequence of yyyy was converted to ̤ because ̤ and yyyy are the same amount of delay, but 2 bytes cheaper. The loop exit was also combined with the HLORW main program segment in order to save on the spacing bytes (12 bytes).

| improve this answer | |

Perl 5, 103 91 85 bytes

map{my%l;@l{/./g}++;@k{/./g}=keys%l}AJK,BCDEGPTVZ,SXF,IY,QU,MN;say map$k{$_}||$_,A..Z

Try it online!

This code (ab)uses the fact that Perl's output of hash keys (%l) is random to create a mapping (%k) of all the modifiable letters to one of their possible counterparts. At output time, any key that doesn't exist is assumed to be unchanged.

| improve this answer | |
  • \$\begingroup\$ Except Perl's output of hash keys isn't random at all. It's completely deterministic, just influenced by the keys themselves in a way that makes it difficult to predict. That's why this code produces the same output on every run. Dunno whether that disqualifies this approach or not. \$\endgroup\$ – John Bollinger Jul 11 '19 at 20:49
  • \$\begingroup\$ @JohnBollinger That is only true within a run of a program. Within a single run, the hash order will be the same if the hash is unmodified. Across two runs or with a modification, there is a random seed created during each invocation of perl. Reference \$\endgroup\$ – Xcali Jul 11 '19 at 21:23
  • \$\begingroup\$ Ok, @Xcali, I stand corrected. I have been at least partially misled, however, by your "Try it online!" link, which repeatedly generates the same output for me. It must be caching, or something. \$\endgroup\$ – John Bollinger Jul 11 '19 at 21:27
  • \$\begingroup\$ Using keys is definitely a good approach, but you can save 6 bytes using sort rand 2,... instead :( Try it online! \$\endgroup\$ – Dom Hastings Jul 18 '19 at 8:10

Jelly, 34 bytes


Try it online!

| improve this answer | |

Python 3, 149 bytes

a,b,i,q,s,m,h,l,o,r,w=(set(s)for s in["AJK","BCDEGPTVZ","IY","QU","SXF","MN",*"HLORW"])
print([eval(c).pop()for c in[*"abbbbsbhiaalmmobqrsbqbwsib"]])

Try it online!

Randomization by using pop() for letter set

| improve this answer | |

APL (Dyalog Extended), 55 bytes

Full program. Prints uppercase with a leading and trailing space, but no intermediary spaces.


Try it online!

⎕A the uppercase alphabet

'AjkBcdegptvzIyQuSxfMn'() apply the following anonymous tacit function with that as right argument and the indicated string as left argument:

 for the left argument,

 partition it, beginning a new segment where

 the left arguments characters are members of the right argument (i.e. on uppercase letters)

,⍨ append

 enclose (to treat it as a single element)
 right argument

 uppercase everything

{}/ reduce by the following anonymous lambda, giving …"QU"λ("SXF"λ("MN"λ"A-Z")):

⊢⍵ on the right argument (the scrambling-in-progress alphabet)

()@(∊∘⍺) apply the following anonymous tacit function to the subset that is a member of the left argument (a rhyme group)

   on that subset

   reorder it to be

  ?⍨ a random permutation
   of length
   tally of letters in the subset

| improve this answer | |

Charcoal, 43 bytes

FαF⪪”&↖(vJf#S»↖ιηa↷N↖⪪νP´↑x‖υ” F№κι‽Φκ¬№KAμ

Try it online! Link is to verbose version of code. Charcoal has no shuffling operators, but I came up with a method of sampling without replacement. Explanation:


Loop over each letter of the alphabet.


Split the string AJK BCDEGPTVZ IY QU SXF MN H L O R W on spaces and loop over the substrings.


Loop over the number of times the current letter appears in the substring. (I use a loop because a conditional would need an else caluse. Alternatively I could have filtered on the substring containing the current letter for the same byte count.)


Print a random character but exclude ones that have already been printed.

| improve this answer | |

Retina, 80 bytes

[blank line]
[blank line]

Try it online!

Probably not the most golfed method, but I'll submit it anyway.



Set the working string to 1A2B2C2D2E5F2GH3I1J1KL6M6NO2P4QR5S2T4U2VW5X3Y2Z. There is a number before each letter in a group, for example A, J and K all have 1 before them.


Mark a section of code that will produce some retina code, then run it afterwards.


Set the working string to 123456


Replace each character with ?O`{character}.¶

[blank line]

Remove the trailing newline and finish the group to generate the code. The group will generate the code:


{n}. matches all instances of number n followed by a character. ?O sorts each instance randomly, and this is done for all character sets.

[blank line]

Finally, remove all numbers and implicitly output the generated string.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.