Generate a pronounceable word

16

3

The challenge is simple:

generate a word.

Specifications:

  • Word must be pronounceable.
    • This is defined as "alternating between a consonant and a vowel."
    • A consonant is one of the following letters: bcdfghjklmnpqrstvwxz
    • A vowel is one of the following letters: aeiouy
  • Word must be randomly generated.
  • Words must be able to contain every consonant and vowel. (You can't just use bcdf for consonants and aei for vowels.)
  • Word must contain 10 letters.
  • Shortest code (in character count) wins.

Doorknob

Posted 2013-06-18T02:23:28.723

Reputation: 68 138

Would something like this be allowed ? alert('abecidof'+'uy'[Math.round(Math.random())]+'ghijklmnpqrstvwxz'[Math.round(Math.random()*19)]); – dwana – 2015-02-16T15:05:32.587

1@Kartik depends on the context, in 'yes' it's a consonant, in 'why' it's a vowel, but this would make it impossible to define a pronounceable word as alternating between vowels and consonants, eg. yyyyyyyy would be a valid word. – CJStuart – 2015-02-17T22:30:14.103

@JanDvorak Okay... what does that have to do with this? – Doorknob – 2013-06-18T22:15:40.343

7

With this xkcd strip in mind, the program echo buxitiwymu technically conforms to the specification. I assure you, I generated the word randomly:P

– AardvarkSoup – 2013-06-23T22:02:58.387

1@AardvarkSoup "Words must be able to contain every consonant and vowel" – Doorknob – 2013-06-23T22:52:34.537

@Ragnagord No, it won't. But, yyyyyyyyyy would. – Erik the Outgolfer – 2016-09-28T17:42:22.193

1I actually made a generator on Scratch a while back. It had specific rules for when you can treat y as a vowel, where you can use q and x, and when you can use two-letter combinations like ng or ea – Esolanging Fruit – 2016-11-06T21:42:02.077

y is a vowel !? – Kartik – 2013-09-16T15:13:36.517

Answers

7

GolfScript, 32 characters

['aeiouy'.123,97>^]5*{.,rand=}%+

Run it online.

Howard

Posted 2013-06-18T02:23:28.723

Reputation: 23 109

8

Ruby: 56 characters

([v=%w(a e i o u y),[*?a..?z]-v]*5).map{|a|$><<a.sample}

Example outputs:

  • itopytojog
  • umapujojim
  • ipagodusas
  • yfoqinifyw
  • ebylipodiz

Paul Prestidge

Posted 2013-06-18T02:23:28.723

Reputation: 2 390

1'aeiouy'.chars would be one char shorter. – Howard – 2013-06-19T06:01:06.923

@Howard then the subtraction operator raises TypeError: can't convert Enumerator into Array – John Dvorak – 2013-06-19T06:30:09.383

@JanDvorak Sorry, forgot to mention that you need Ruby 2.0 for this trick. – Howard – 2013-06-19T06:38:17.490

7

Python, 81

from random import*
print''.join(map(choice,["bcdfghjklmnpqrstvwxz","aeiouy"]*5))

Good luck pronouncing them.

grc

Posted 2013-06-18T02:23:28.723

Reputation: 18 565

They're actually quite easy to pronounce, for example the first word I got was "viketuziwo" :P – Doorknob – 2013-06-18T03:16:37.483

@Doorknob maybe it's just my luck. I keep getting 'words' like "qijepyjyga". My computer's attempts to pronounce them make up for it though :) – grc – 2013-06-18T05:04:44.253

3I just had a lot of fun doing python grc.py | say on my machine. Thanks for the idea. – Kaya – 2013-06-21T03:07:14.610

7

COBOL, 255

So I'm learning COBOL at the moment. Used this question as some practice. Tried to golf it.

It's 255 without the leading whitespace, and 286 bytes with.

For what it's worth, this runs in Microfocus COBOL for VS2012, and I have no idea if it will run anywhere else.

       1 l pic 99 1 s pic x(26) value'aeiouybcdfghjklmnpqrstvwxz' 1 r
       pic 99 1 v pic 9 1 q pic 99. accept q perform test after varying
       l from 1 by 1 until l>9 compute v=function mod(l,2) compute r=1+
       function random(q*l)*5+v*15+v*5 display s(r:1)end-perform exit

Cereal

Posted 2013-06-18T02:23:28.723

Reputation: 299

6

APL (34)

,⍉↑(V[5?6])((⎕A~V←'AEIOUY')[5?20])

marinus

Posted 2013-06-18T02:23:28.723

Reputation: 30 224

6

JavaScript, 74

for(a=b=[];a++-5;)b+="bcdfghjklmnpqrstvwxz"[c=new Date*a%20]+"aeiouy"[c%6]

Does not generate all combinations, but I think that all consonants and vowel appear.

JavaScript, 79

for(a=b=[];a--+5;)b+="bcdfghjklmnpqrstvwxz"[c=Math.random()*20^0]+"aeiouy"[c%6]

More "random" version.

copy

Posted 2013-06-18T02:23:28.723

Reputation: 6 466

What's the ^0 for? – Alpha – 2013-06-25T03:01:13.577

1@Alpha Math.random gives a float, we need an integer. ^0 truncates the number – copy – 2013-06-25T11:52:31.243

Clever trick. I haven't seen the ^ operator in JavaScript before and less heard about using it to truncate a float. Thanks! – Alpha – 2013-06-25T17:06:37.467

@copy i like the use of ^ – Math chiller – 2013-10-02T03:16:05.613

4

Ruby: 70 66 characters

10.times{|i|$><<["aeiouy"*4,"bcdfghjklmnpqrstvwxz"][i%2][rand 20]}

Sample run:

bash-4.1$ ruby -e '10.times{|i|$><<["aeiouy"*4,"bcdfghjklmnpqrstvwxz"][i%2][rand 20]}'
izogoreroz

bash-4.1$ ruby -e '10.times{|i|$><<["aeiouy"*4,"bcdfghjklmnpqrstvwxz"][i%2][rand 20]}'
onewijolen

bash-4.1$ ruby -e '10.times{|i|$><<["aeiouy"*4,"bcdfghjklmnpqrstvwxz"][i%2][rand 20]}'
amilyfilil

manatwork

Posted 2013-06-18T02:23:28.723

Reputation: 17 865

You can use 10.times for one char less. – Howard – 2013-06-19T04:06:04.147

1Also the question doesn't require that each letter must have same probability. *10->*4, skip *3, rand 60->rand 20 and you have saved 3 chars. – Howard – 2013-06-19T04:10:09.257

Good catch on the rule, @Howard. Thank you. – manatwork – 2013-06-19T07:18:35.827

4

Processing, 100 99 93 87

int i=10;while(i-->0)"aeiouybcdfghjklmnpqrstvwxz".charAt((int)random(i%2*6,6+i%2*i*2));

Upon closer inspection of the question, I see it doesn't require any output. I've adjusted this accordingly.

Cereal

Posted 2013-06-18T02:23:28.723

Reputation: 299

4

R: 105 characters

a=c(1,5,9,15,21,25)
l=letters
s=sample
cat(apply(cbind(s(l[-a],5),s(l[a],5)),1,paste,collapse=""),sep="")

plannapus

Posted 2013-06-18T02:23:28.723

Reputation: 8 610

4

J (51)

,|:>(<"1[5?6 20){&.>'aeiouy';'bcdfghjklmnpqrstvwxz'

marinus

Posted 2013-06-18T02:23:28.723

Reputation: 30 224

3

Javascript, 135 122 96 86 characters

s='';c=5;r=Math.random;while(c--)s+='bcdfghjklmnpqrstvwxz'[r()*20|0]+'aeiouy'[r()*6|0]

Doorknob

Posted 2013-06-18T02:23:28.723

Reputation: 68 138

3

Javascript, 85

for(r=Math.random,s="",i=5;i--;)s+="bcdfghjklmnpqrstvwxz"[20*r()|0]+"aeiouy"[6*r()|0]

If run from the console, output is shown. Explicit display would add alert(s) at 8 chars, still shorter than the other JS solutions.

Thanks C5H8NNaO4 and Howard!

tomsmeding

Posted 2013-06-18T02:23:28.723

Reputation: 2 034

Nice one, save a character by removing the last ';' – C5H8NNaO4 – 2013-06-18T11:20:07.173

1Instead of ~~(###) you can write ###|0 which saves 4 chars. – Howard – 2013-06-19T12:44:27.147

3

Java AKA the most verbose language ever created, 176 with help of Doorknob, Daniero and Peter Taylor (thanks guys!)

class w{public static void main(String[]a){int i=11;while(--i>0)System.out.print((i%2==0?"aeiouy":"bcdfghjklmnpqrstvwxz").charAt(new java.util.Random().nextInt(6+(i%2)*14)));}}

Ungolfed:

    class w {

        public static void main(String[] a) {
            int i = 11;
            while (--i > 0) {
                System.out.print((i % 2 == 0 ? "aeiouy" : "bcdfghjklmnpqrstvwxz").charAt(new java.util.Random().nextInt(6 + (i % 2) * 14)));
            }
     }

}

jsedano

Posted 2013-06-18T02:23:28.723

Reputation: 1 607

1Suggested improvements: Change String a[] to String[]a (-1 characters), change w W = new w(); to w W=new w(); (-2 characters) – Doorknob – 2013-06-18T22:14:49.057

Suggestions: Let the word always start on a consonant (or wovel); no need to randomize this when the question doesn't mention it! So, skip the boolean f and use i%2 instead. Also, the for loop can be shortened, and you can put both the strings inside the conditional operator (also,no need for parens here), and use the charAt on the outside. Here's the whole thing, 195 CHARS, 38 SAVED: import java.util.*;class w{public static void main(String[]a){Random r=new Random();for(int i=0;++i<11;)System.out.print((i%2>0?"bcdfghjklmnpqrstvwxz":"aeiouy").charAt(r.nextInt(6+(i%2)*14)));}} – daniero – 2013-06-19T01:40:37.233

Continuing @Daniero's simplification: 1) do not declare a variable for Random; 2) remove the parenthesis from the formula. 183 characters: import java.util.*;class w{public static void main(String[]a){for(int i=0;++i<1100;)System.out.print((i%2>0?"bcdfghjklmnpqrstvwxz":"aeiouy").charAt(new Random().nextInt(6+i%2*14)));}}. – manatwork – 2013-06-19T07:12:44.630

2

“Java AKA the most verbose language ever created” – Ever tried Shakespeare? ;-)

– manatwork – 2013-06-19T07:13:59.760

1@manatwork, don't forget to remove the import once you've got it down to being used only in one place. – Peter Taylor – 2013-06-19T11:52:17.540

Possibly controversial suggestion: One could squeeze a few more characters out of it by insisting that the program has to be run with two command line arguments (that should be counted in the character total of course), namely aeiouy and bcdfghjklmnpqrstvwxz. This minimizes the overhead of the strings as you don't need the "'s anymore, and you can access them with a[i%2] (a is already defined in the boilerplate code). 142+20(?) chars: class w{public static void main(String[]a){int i=11;while(--i>0)System.out.print(a[i%2].charAt(new java.util.Random().nextInt(6+(i%2)*14)));}} – daniero – 2013-06-19T16:40:33.827

@Daniero, why only +20? Shouldn't that be +6+20? And no idea why you (and anakata) insist on the parenthesis around i%2. Its priority is higher than the preceding addition's anyway. http://pastebin.com/5reTnRDL

– manatwork – 2013-06-20T06:26:18.687

@manatwork, 26 yes off course, I did this very rapidly, just vaguely remembered that some string in there was 20 chars long (my native language's alphabet is longer than the English one, so 26 doesn't mean that much to me) ;) The parenthesis can obviously go, nice. The first time I used the expression 14*(i%2), in which the parens must stay, didn't think off switching 14 to the back. Last time I just copied the whole thing from anakata's answer to show the a[i%2] trick, didn't think about the parenthesis any longer. – daniero – 2013-06-20T15:11:01.807

1@manatwork maybe we like lisp ?? ok, sorry, gonna take those parenthesis out when I get home from work – jsedano – 2013-06-20T15:18:38.243

I know it's been 6 years (lol), but you can golf it to 163 bytes by changing int i=11;while(--i>0) to for(int i=11;--i>0;); i%2==0 to i%2<1 and new java.util.Random().nextInt(6+(i%2)*14) to (int)(Math.random()*(6+i%2*14)). EDIT: Actually, 160 bytes like this: class W{public static void main(String[]a){for(int i=11,t;--i>0;System.out.print((i%2<1?"aeiouy":"bcdfghjklmnpqrstvwxz").charAt(t*=Math.random())))t=6+i%2*14;}}, or an additional -3 with Java 8+ changing class W{public static void main to interface W{static void main. Try it online.

– Kevin Cruijssen – 2019-05-03T12:34:41.817

3

Objective-C, 129

main(){int i=10;while(i-->0)printf("%c",[(i%2==0)?@"aeiouy":@"bcdfghjklmnpqrstvwxz"characterAtIndex:arc4random()%(6+(i%2)*14)]);}

With the help of Daniero

(I love to use the tends to operator (-->)

jsedano

Posted 2013-06-18T02:23:28.723

Reputation: 1 607

3

PHP, 100 Characters

<?while($i<6){echo substr('bcdfghjklmnpqrstvwxz',rand(0,20),1).substr('aeiouy',rand(0,5),1);$i++;}?>

Juan José Sedano

Posted 2013-06-18T02:23:28.723

Reputation: 129

acually I think that's 100 chars long – Cristian Lupascu – 2013-07-24T11:27:48.167

3

Unix tools: 73 bytes

And not guaranteed running time :)

</dev/urandom grep -ao '[aeiouy][bcdfghjklmnpqrstvwxz]'|head -5|paste -sd ''

Only problem is that the generated string will start with a "vowel" every time.

(edit: changed ' ' to '' in the args of paste) (another edit: removed -P from grep options, thanks to manatwork)

pgy

Posted 2013-06-18T02:23:28.723

Reputation: 830

Something could be fine tuned around the grep parameters. I got “of e� ap ag ak”. – manatwork – 2015-02-04T13:56:48.443

Hmm... strange. I got nothing like that. I thought -a would be enough. – pgy – 2015-02-04T13:58:39.883

1My test indicates that -P is the one. Seems the man warns about its highly experimental status with a reason. (grep 2.16) But anyway, it works fine without -P. – manatwork – 2015-02-04T14:01:31.857

You are right thank you, I didn't consider that. I don't even know why I used -P in the first place. I'll edit my answer. – pgy – 2015-02-04T14:03:44.353

1By the way, tr -d \\n is shorter for joining the lines. – manatwork – 2015-02-04T14:04:21.277

yes, but it also removes the last '\n' messing up bash prompt :) – pgy – 2015-02-04T14:07:00.397

Doh. Indeed. And the “grep: write error” gets concatenated with the output. :( – manatwork – 2015-02-04T14:16:01.943

3

APL 30 26

,('EIOUY'∪⎕A)[6(|,+)⍪5?20]

Explanation is very similar to the past version below, just reordered a bit to golf the solution.

Note: ⎕IO is set to 0


('EIOUY'∪⎕A)[,6(|,(⍪+))5?20]

Explanation:

'EIOUY'∪⎕A    puts vowels in front of all letters.
5?20            for the indexes we start choosing 5 random numbers between 0 and 19
6(|,(⍪+))        then we sum 6 and the random numbers, convert to 5x1 matrix (⍪), add a column before this one containing 6 modulo the random numbers. 
                [[[ this one can be rewritten as: (6|n) , ⍪(6+n)  for easier understanding]]]
,6(|,(⍪+))5?20  the leading comma just converts the matrix to a vector, mixing the vowel and consonants indexes.

Tryapl.org

Moris Zucca

Posted 2013-06-18T02:23:28.723

Reputation: 1 519

1('AEIOUY'∪⎕A) ≡ (∪'AEIOUY',⎕A) but it's one byte shorter. – lstefano – 2016-06-30T12:42:42.070

1Deferring 'A' to ⎕A saves another byte: ,('EIOUY'∪⎕A)[6(|,+)⍪5?20] – Adám – 2016-07-03T04:03:03.770

Nice! down to 26. Thanks – Moris Zucca – 2016-07-04T07:54:08.737

3

Pyth, 26 characters

J-G"aeiouy"VT=k+kOJ=J-GJ;k

You can try it in the online compiler here.

Someone posted a very similar challenge but it was closed after I had made a solution. I didn't realize it, but this question actually predates the creation of Pyth. Anyway, here is the breakdown:

J                             Set string J equal to:
  G                            the entire alphabet (lowercase, in order)
 - "aeiouy"                    minus the vowels
           VT                 For n in range(1, 10):
             =k                   Set string k to:
                k                  k (defaults to empty string)
               + OJ                plus a random character from J
                   =J             Set J to:
                      G            the entire alphabet
                     - J           minus J
                        ;     End of loop
                         k    print k

Every time the loop is run, J switches from being a list of consonants to a list of vowels. That way we can just pick a random letter from J each time.
There may be a way to initialize J in the loop or remove the explicit assignments from the loop, but I have not had success with either yet.

Mike Bufardeci

Posted 2013-06-18T02:23:28.723

Reputation: 1 680

1Bumping old questions is generally considered fine. However, when you use a language newer than the question (I created Pyth in 2014) you should note this in your answer. – isaacg – 2015-07-07T03:42:20.907

Thanks for clearing that up for me. I didn't realize that Pyth was created after this question and I've added that to the answer. – Mike Bufardeci – 2015-07-07T04:11:07.410

2

PHP 79 bytes

<?for($c=bcdfghjklmnpqrstvwxz,$v=aeiouy;5>$i++;)echo$c[rand()%20],$v[rand()%6];

Fairly concise.

primo

Posted 2013-06-18T02:23:28.723

Reputation: 30 891

2

Javascript, 104 87

a="";for(e=10;e--;)a+=(b=e&1?"bcdfghjklmnpqrstvwxz":"aeiouy")[0|Math.random()*b.length]

golfed a whole lot of simple unnecessary stuff, still not nearly as nice as copys' one

Oh, and that one just opped up during golfing: "dydudelidu"

Now I tried one using the 2 characters at once approach. Turns out it's almost the same as copys' second one, so I can't count it, also at 79. a="";for(e=5;e--;)a+="bcdfghjklmnpqrstvwxz"[m=0|20*Math.random()]+"aeiouy"[m%6]

C5H8NNaO4

Posted 2013-06-18T02:23:28.723

Reputation: 1 340

2

C: 101

main(){int x=5;while(x-->0){putchar("bcdfghjklmnpqrstvwxyz"[rand()%21]);putchar("aeiou"[rand()%5]);}}

Kevin

Posted 2013-06-18T02:23:28.723

Reputation: 501

2

Brachylog, 9 bytes

Ḍ;Ẉṣᵐzh₅c

Try it online!

Gives output as a list of letters through the output variable.

   ṣ         Randomly shuffle
 ;  ᵐ        both
Ḍ            the consonants without y
  Ẉ          and the vowels with y,
     z       zip them into pairs,
      h₅     take the first five pairs,
             and output
        c    their concatenation.

Unrelated String

Posted 2013-06-18T02:23:28.723

Reputation: 5 300

1

F#, 166 characters

open System;String.Join("",(Random(),"aeiouybcdfghjklmnpqrstvwxz")|>(fun(r,s)->[0..5]|>List.collect(fun t->[s.Substring(6+r.Next()%20,1);s.Substring(r.Next()%6,1)])))

Shredderroy

Posted 2013-06-18T02:23:28.723

Reputation: 111

1

05AB1E, 10 bytes

5FžPΩ?žOΩ?

Try it online or output \$n\$ amount of random words.

Explanation:

Pretty straight-forward:

5F      # Loop 5 times:
  žP    #  Push string "bcdfghjklmnpqrstvwxz"
    Ω   #  Pop and push a random character from this string
     ?  #  Output it without newline
  žOΩ?  #  Do the same with string "aeiouy"

Kevin Cruijssen

Posted 2013-06-18T02:23:28.723

Reputation: 67 575

1

K, 40 bytes

,/+5?/:("bcdfghjklmnpqrstvwxz";"aeiouy")

5?"abc" will generate 5 random letters from the given string.

5?/: will generate 5 random letters from each of the strings on the right, producing two lists.

+ transposes those two lists, giving us a list of tuples with one random character from the first list and then one from the second list.

,/ is "raze"- fuse together all those tuples in sequence.

K5 can do this in 33 bytes by building the alphabet more cleverly and then using "except" (^) to remove the vowels, but K5 is much too new to be legal in this question:

,/+5?/:((`c$97+!26)^v;v:"aeiouy")

JohnE

Posted 2013-06-18T02:23:28.723

Reputation: 4 632

1

R, 83 bytes

cat(outer((l<-letters)[a<-c(1,5,9,15,21,25)],l[-a],paste0)[sample(1:120,5)],sep="")

Generate all possible vowel-consonant sequences in a matrix, then randomly sample 5 of them, yielding a 10-letter word.

rturnbull

Posted 2013-06-18T02:23:28.723

Reputation: 3 689

0

Jelly, 13 bytes

Øy,ØYẊ€Zs5ḢŒl

Explanation:

Øy,ØYẊ€Zs5ḢŒl
Øy,ØY         Makes a list of two lists of characters: [[list-of-vowels-with-y], [list-of-consonants-without-y]]
     Ẋ€       Shuffle €ach.
       Z      Zip those two lists together. [[random-vowel, random-consonant],...]
        s5    Split them into chunks of five (because each pair contains two letters, this splits them into chunks of 10 letters)
          Ḣ   Take the list of 5 pairs.
           Œl Make each of the characters in it lowercase
              Implicit concatenation and print.

Try it online!

Comrade SparklePony

Posted 2013-06-18T02:23:28.723

Reputation: 5 784