In one of our projects at work, we recently discovered a particularly large method for generating a 6 character string from a 15 character alphabet. A few of us claimed "I bet we can get that in one line" which started a little internal game of code golf.

Your task is to beat us, which I have no doubt won't take long!

The original algorithm used the alphabet 0-9A-E, but we've experimented with other alphabets. There are therefore three subtasks.

  1. Generate a 6 character string randomly selecting from an arbitrary hardcoded 15 character alphabet like ABC123!@TPOI098. (This is just an example, and should be customizable without affecting the byte count.)
  2. Generate a 6 character string randomly selecting from a 15 character alphabet 0123456789ABCDE.
  3. Generate a 6 character string randomly selecting from a 15 character alphabet of your choice (printable characters only please).

Each character should have equal chance of selection and repetition should be possible.

The best we have been able to manage for each of the subtasks is:

  • "ABC123!@TPOI098" - 24 bytes
  • "0123456789ABCDE" - 21 bytes
  • Custom alphabet - 13 bytes

Your score is the sum of the bytes in each subtask's solution. i.e. our score is currently 58.

We've attempted using among others, CJam and Ruby. The original was in C#. Use any language you like, but we'll be interested to see solutions in these languages particularly

  • 5
    \$\begingroup\$ Regarding multi-part challenges. Unfortunately, I don't have a good solution in this case, since these three subtasks are way too similar for it to make sense to split them up into multiple challenges. I've also been considering proposing an exception to that policy for multi-part challenges where the subtasks are just minor variations of the same challenge. (Although that still has the problem that sub-solutions can be taken from other answers.) So I won't mod-hammer this, and see what the community thinks. \$\endgroup\$ – Martin Ender Oct 15 '16 at 13:51
  • \$\begingroup\$ "Your score is the sum of the bytes..." so my first example is unfortunate. I'll amend to another potential example \$\endgroup\$ – James Webster Oct 15 '16 at 14:11
  • 2
    \$\begingroup\$ @MartinEnder My 2 cents: I think it's OK and will not VTC. Sure, I generally think a challenge with a single interesting task is better, but since these tasks are very similar, it's a lot better than a "golf-course" that says "do these 8 random unrelated tasks." Even though there is no interaction between tasks, in my eyes this challenge is not very different than say Golf all 16 logic gates. \$\endgroup\$ – James Oct 15 '16 at 14:41
  • \$\begingroup\$ Are there any time or memory limits? Do the subtasks have to be independent or are they allowed to share code? \$\endgroup\$ – Dennis Oct 15 '16 at 14:41
  • 2
    \$\begingroup\$ Does "generate a string" mean the code has to actually generate a string value with the appropriate characters in it, or is outputting the six characters (not separated by space or newlines) acceptable? \$\endgroup\$ – DLosc Oct 15 '16 at 19:17

21 Answers 21


Jelly, 38 bytes

TryItOnline links A, B, and C.

A: ABC123!@£POI098, 22 bytes


(thinking about a compression to lessen this one)

B: 0123456789ABCDE, 8 bytes:


C:123456789ABCDEF (choice), 8 bytes:



...Wẋ6X€ - common theme
   W     - wrap (string) in a list
    ẋ6   - repeat six times
      X€ - random choice from €ach

ØH...... - hexadecimal digit yield: "0123456789ABCDEF"

..Ṗ..... - pop: z[:-1] (B)

..Ḋ..... - dequeue: z[1:] (C)

CJam (23 + 14 + 10 = 47 bytes)

Arbitrary alphabet: 23 bytes (online demo)


Hexadecimal alphabet: 14 bytes (online demo)


Custom alphabet: ABCDEFGHIJKLMNO, 10 bytes (online demo)



The hexadecimal one is the interesting one:

{      e# Loop...
  Fmr  e#   Select a random number from 0 to 14
  AbHb e#   Convert to base 10 and then to base 17
       e#   (i.e. add 7 if the number is greater than 9)
  '0+  e#   Add character '0' (i.e. add 48 and convert from integer to character)
       e#   Note that 'A' - '0' = 17
}6*    e# ...six times

The six characters are left on the stack and printed automatically.

  • 2
    \$\begingroup\$ AbHb is brilliant. I thought about that general approach but _9>7*+ was too long. \$\endgroup\$ – Martin Ender Oct 15 '16 at 15:00

Perl, 46 + 26 + 26 = 98 bytes

A lot of the credit goes to @Dom Hastings for saving 13 bytes!

The 3 programs are pretty much identical, except for the alphabet that changes.

  • Hardcoded alphabet (ABC123!@)POI098 in this example) -> 46 bytes:

    say map{substr"ABC123!@)POI098",15*rand,1}1..6

  • Fixed alphabet 0123456789ABCDE -> 26 bytes:

    printf"%X",rand 15for 1..6

  • Custom alphabet 0123456789ABCDE in that case -> 26 bytes:

    printf"%X",rand 15for 1..6

You can put them all in a file to run them :

$ cat 6chr_strings.pl
say map{substr"ABC123!@)POI098",15*rand,1}1..6;
say "";
printf"%X",rand 15for 1..6;
say "";
printf"%X",rand 15for 1..6;
say "";
$ perl -M5.010 6chr_string.pl

(the say ""; are just here to improve the output format)

  • 2
    \$\begingroup\$ Good answers! I had pretty much the same for the first and last, but you can save a byte using say: say map{("ABC123!@)POI098"=~/./g)[rand 15]}1..6 and say map{(A..O)[rand 15]}1..6. For the second you can use printf: printf"%X",rand 15for 1..6 to save 11 over all! I'm sure Ton can advise on some arcane magic to save more too! \$\endgroup\$ – Dom Hastings Oct 17 '16 at 14:40
  • 1
    \$\begingroup\$ Actually with the custom alphabet substr saves another: say map{substr"ABC123!@)POI098",15*rand,1}1..6 \$\endgroup\$ – Dom Hastings Oct 17 '16 at 14:42
  • 1
    \$\begingroup\$ @DomHastings Hmm indeed, that's nice, well played! Thanks :-) \$\endgroup\$ – Dada Oct 17 '16 at 15:06
  • 2
    \$\begingroup\$ I like that you removed the note about the code being straightforward :D \$\endgroup\$ – Dom Hastings Oct 17 '16 at 15:32
  • \$\begingroup\$ @DomHastings with printf"%X", substr..rand and map, it's just a bit less obvious, so I let people enjoy perl magic with no spoilers! :D \$\endgroup\$ – Dada Oct 17 '16 at 15:36

R, 33+43+59 = 135 bytes

Arbitrary hard-coded alphabet (change the string to change the alphabet):


Alphabet of [0-9A-E]:


User-defined alphabet from stdin:


All cases print the output word to stdout.


JavaScript (ES6), 167 166 164 163 bytes

Saved 1 byte thanks to Neil
Saved 2 bytes thanks to ETHproductions
Saved 1 byte thanks to premek.v

Hardcoded: "ABC123!@TPOI098" (58 bytes)


Fixed: "0123456789ABCDE" (58 57 bytes)


Custom: "()+.1=>?M[afhnt" (51 49 48 bytes)

  • 1
    \$\begingroup\$ (n=6)=>n?f(n-1)+("ABCDE"[n=Math.random()*15|0]||n-5):'' saves you a byte. \$\endgroup\$ – Neil Oct 15 '16 at 17:58
  • \$\begingroup\$ 1/8+Math is great :) \$\endgroup\$ – ETHproductions Oct 16 '16 at 0:52
  • \$\begingroup\$ But .1+JSON is better ;) \$\endgroup\$ – ETHproductions Oct 16 '16 at 13:49
  • 1
    \$\begingroup\$ Or JSON+f ([object JSON](n => " (JNOS[]bcejnot") \$\endgroup\$ – ETHproductions Oct 16 '16 at 13:53
  • \$\begingroup\$ @ETHproductions Nice one. :) \$\endgroup\$ – Arnauld Oct 16 '16 at 14:05

JavaScript (ES6), 184 bytes

Custom alphabet: 66 bytes


0-9A-E: 63 bytes


0-9a-e: 55 bytes


(Subtract 6 bytes if date-based randomness is permissible.)

  • \$\begingroup\$ You can save a byte on the last one with **: _=>((Math.random()+1)*15**6|0).toString(15).slice(1) \$\endgroup\$ – ETHproductions Oct 16 '16 at 14:21
  • \$\begingroup\$ @ETHproductions Surely that would make it ES7, rather than ES6? (Also, it looks like a 3-byte saving to me.) \$\endgroup\$ – Neil Oct 16 '16 at 21:21
  • \$\begingroup\$ Yes, and it does appear to save 3 bytes. I must have included the f= in the byte count \$\endgroup\$ – ETHproductions Oct 20 '16 at 2:39

q, 42 bytes


19 bytes



14 bytes



9 bytes


(uses the first fifteen letters of the alphabet)


Julia (36+26+21 = 83)




CJam, 48 bytes

Arbitrary alphabet, 23 bytes:


Try it online!

Hex digits, 15 bytes:


Try it online!

Alphabet ABCDEFGHIJKLMNO, 10 bytes:


Try it online!

  • \$\begingroup\$ I had a bad idea. If we consider U+FFFE a character, ~c instead of 'A+ returns something technically printable. \$\endgroup\$ – jimmy23013 Oct 16 '16 at 15:34
  • \$\begingroup\$ Maybe I'm wrong. I didn't find a definition of printable Unicode characters. \$\endgroup\$ – jimmy23013 Oct 16 '16 at 15:38

Ruby 47 + 37 + 31 = 115

Hardcoded: "ABC123!@TPOI098" (47)


Fixed: "0123456789ABCDE" (37)




05AB1E, 43 bytes

Arbitratry alphabet (ABC123!@TPOI098), 23 bytes Try it online


Almost hexadecimal alphabet (0123456789ABCDE), 10 bytes Try it online


Custom alphabet (abcdefghijklmno), 10 bytes Try it online


Python 2, 70 + 70 + 64 = 204 bytes

from random import*
print s

from random import*
print s

from random import*
print s

Unfortunately, the second example is easier with the first method than something like choice([randint(48,57)),choice(65,69)])

  • \$\begingroup\$ Why do you use from random import*? I think you can use import random and random.choice at least in the first two examples. \$\endgroup\$ – Roman Gräf Oct 15 '16 at 18:27
  • \$\begingroup\$ import random random.choice is 27 but from random import* choice is 26, also import random as r r.choice is 27 \$\endgroup\$ – Karl Napf Oct 15 '16 at 19:26
  • \$\begingroup\$ For the hex case, we can do a bit better by using format(randrange(8**8),'X'), I think. \$\endgroup\$ – DSM Oct 16 '16 at 15:37
  • \$\begingroup\$ @DSM the problem is, there must be no F \$\endgroup\$ – Karl Napf Oct 16 '16 at 20:32

Vitsy, 27 + 24 22 + 13 10 = 64 62 59 bytes



Try it online!



Try it online!



Where the available characters are:


Try it online!


J, 24 + 24 + 18 10 = 58 bytes

8 bytes saved thanks to miles!


Yeah, the second string isn't easily compressible in J:

u:47+23#.inv 12670682677028904639x
u:47+;(+i.@])/"1&.>1 10;18 5

If a lowercase hex alphabet is fine, then there's ,hfd?6#15 for 9 bytes, as @miles noted.

Anyhow, ?6#15 is 6 random numbers between 0 and 15; {~ is take-from. u: converts numbers to chars. The last example encodes ABCDEFGHIJKLMNOP.

Bonus: general case


{~6?@## is roughly:

{~6?@##  input: y
      #  length of y
  6  #   six copies of the length
   ?@    random numbers between 0 and the length
{~       taken from y
  • \$\begingroup\$ On the second case, there's a builtin named hfd which converts to hex from decimal. You can get a 9 byte solution using ,hfd?6#15. The last case, just to have something easily read, uses the alphabet from 'A' for a 10 byte solution u:65+?6#15, making a total 24 + 9 + 10 = 45. \$\endgroup\$ – miles Oct 17 '16 at 3:51
  • \$\begingroup\$ @miles I think the second case requires capitalized letters. As for the last case... haha, oops. I completely forgot about vectorized addition. \$\endgroup\$ – Conor O'Brien Oct 17 '16 at 11:08

PHP, 46+36+35=117 bytes

Hardcoded (46 bytes)


(47 bytes)


Hexadecimal (lowercase) (36 bytes)

for(;$j++<6;)echo dechex(rand()%15);

For uppercase, 46 bytes with Hardcoded version.

Custom (A-O) (35 bytes)

for(;$k++<6;)echo chr(rand(65,79));
  • \$\begingroup\$ I don't think I can accept your 2nd part. a-e isn't the same as A-E \$\endgroup\$ – James Webster Oct 17 '16 at 8:38

Scala, 154 bytes

Hardcoded alphabet (54 bytes):


Hex alphabet (54 bytes):


Custom alphabet ABCDEFGHIJKLMNO (47 bytes):



Seq.fill(6)(               //build a sequence of 6 elements, where each element is...
  "ABC123!@TPOI098"(         //from the string
    (math.random*14).toInt   //take a random char

'A'to'O' creates a sequence of 15 chars, A to O


Pip, 42 bytes

Hardcoded alphabet, 22 bytes:


Hex digits, 11 bytes:


First 15 lowercase letters, 9 bytes:



All three programs start with L6O: loop 6 times and output the given expression.

  • RC"...": Random Choice of a character from the hard-coded string
  • RR15TB16: RandRange(15), converted To Base 16
  • z@RR15: lowercase alphabet z, indexed with RandRange(15)

Try it online!


Skript/skQuery, 108 bytes

Hardcoded (43 bytes):

random 6 char string from `A@cD%F3h9JK{mN!`

0123456789ABCDE (34 bytes):

random 6 char string from `0-9A-E`

Choice (31 bytes):

random 6 char string from `A-M`
  • \$\begingroup\$ Can you move the opening ` to the left? \$\endgroup\$ – Addison Crump Oct 16 '16 at 16:01
  • \$\begingroup\$ @VTCAKAVSMoACE No, it doesn't let you \$\endgroup\$ – Oliver Ni Oct 16 '16 at 16:38

Jolf, 26 + 14 + 13 = 51 bytes

Μ*S6d rG"ABC123!@TPOI098"E

Custom alphabet, 24 bytes. Try it here!

Μ*S6d r lp^0wά

0-9A-E alphabet, 14 bytes. Try it here! lp^0wά is lp (0-Z) sliced (l) from 0 to 15 ().

Μ*S6d r lp^1ά

1-9A-F alphabet, 13 bytes. Try it here! lp^1ά is the same as above, except from 1 to 16.

General method:

Μ*S6d r
M*S6d      map six newlines over this function:
      r    select random element from array.

Other attempts (using string compression):

Μ*S6d rGμpwΞ $AE

PowerShell v2+, 45 + 44 + 37 = 126 bytes

Fixed alphabet, 45 bytes


Nearly-hexadecimal alphabet, 44 bytes


Custom alphabet (A to O), 37 bytes


All of these follow the same pattern -- loop from 0 to 5, each iteration selecting a Random character or ASCII value, casting that as a char-array if necessary, and -joining it together into a string. That string is left on the pipeline, and output is implicit.


PS C:\Tools\Scripts\golfing> -join(0..5|%{'ABC123!@TPOI098'[(Random)%15]})

PS C:\Tools\Scripts\golfing> -join(0..5|%{'ABC123!@TPOI098'[(Random)%15]})

PS C:\Tools\Scripts\golfing> -join(0..5|%{'ABC123!@TPOI098'[(Random)%15]})

PS C:\Tools\Scripts\golfing> -join[char[]](0..5|%{Random(48..57+65..69)})

PS C:\Tools\Scripts\golfing> -join[char[]](0..5|%{Random(48..57+65..69)})

PS C:\Tools\Scripts\golfing> -join[char[]](0..5|%{Random(65..79)})

PS C:\Tools\Scripts\golfing> -join[char[]](0..5|%{Random(65..79)})

Pyke, 35 bytes

Arbitary alphabet, 20 bytes


Try it here!

Hex alphabet, 8 bytes


Try it here!

~J15< - "0123456789abcdefghijklmno..."[:15]

Custom alphabet, 7 bytes


Try it here!

G15< - alphabet[:15]

Alphabet chosen: abcdefghijklmno

6V     - repeat 6 times:
  ...  -   get alphabet
     H -  choose_random(^)
  • \$\begingroup\$ This seems to output 6 chars separated by newline instead of a 6-char string. \$\endgroup\$ – Emigna Oct 17 '16 at 11:26
  • \$\begingroup\$ The question doesn't state the output format. \$\endgroup\$ – Blue Oct 17 '16 at 11:29
  • \$\begingroup\$ I see the words Generate a 6 character string in 4 places in the specification. \$\endgroup\$ – Emigna Oct 17 '16 at 11:30

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.