12
\$\begingroup\$

Your Goal:

Given an odd integer input n greater than 1, generate a random English word of length n. An English word is one in which the odd (1-based) indices are consonants and the even (1-based) indices are vowels, the vowels being aeiou.

Random

For the purposes of this challenge, you are to sample from the vowels and consonants uniformly, with replacement. In other words, each possible English word must be equally likely to appear as an output.

Example Inputs/Outputs

3 -> bab, tec, yiq
5 -> baron, nariz, linog
7 -> murovay, fanafim

Winner:

Winner is the Lowest Byte Count.

\$\endgroup\$
10
  • 4
    \$\begingroup\$ Welcome to the site. I don't really understand the challenge here. Maybe the sandbox (linked on the right), would be a good place to get feedback. \$\endgroup\$
    – Wheat Wizard
    Feb 7, 2020 at 19:19
  • \$\begingroup\$ Are we making a function that generates the words? Or should the program simply generate them when run? \$\endgroup\$
    – sugarfi
    Feb 7, 2020 at 19:49
  • 7
    \$\begingroup\$ Hi there. I've voted to close this as needing additional clarity, but do not fret when this gets closed! It's tough to get a challenge specified up to our standards the first few times you come up with one, so we have a sandbox for you to get feedback on them before they come to the main site. Hope you enjoy your time here, and happy golfing! \$\endgroup\$
    – Giuseppe
    Feb 7, 2020 at 20:46
  • 4
    \$\begingroup\$ Be sure to specify what you mean by random. Also, how is the output size specified? Is it also random? \$\endgroup\$
    – Luis Mendo
    Feb 7, 2020 at 21:35
  • 1
    \$\begingroup\$ I've gone ahead and edited it (and voted to re-open), please make edits to it as you feel appropriate! \$\endgroup\$
    – Giuseppe
    Feb 12, 2020 at 19:19

21 Answers 21

5
\$\begingroup\$

05AB1E, 14 11 bytes

-3 bytes thanks to Kevin Cruijssen

EžN¸žMšNèΩJ

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ ¸ì can be š, .R can be Ω, and the } can be removed before the join. \$\endgroup\$ Feb 10, 2020 at 7:49
  • \$\begingroup\$ oh wow, thank you! @KevinCruijssen \$\endgroup\$
    – mabel
    Feb 10, 2020 at 9:07
5
\$\begingroup\$

Plain English, 485 bytes

To add any vowel to a string:
Pick a letter of the alphabet.
If the letter is any vowel, append the letter to the string; exit.
Repeat.

To add any consonant to a string:
Pick a letter of the alphabet.
If the letter is any consonant, append the letter to the string; exit.
Repeat.

To make a random string given a length:
If a counter is past the length, exit.
Add any consonant to the random string.
If the counter is past the length, exit.
Add any vowel to the random string.
Repeat.

What better way to write some random English than by using Plain English? I have applied a moderate level of golfing to this code; certain things could be shortened, but it would make this less legible, which would be a shame.

The reason I have defined three routines is because nested loops are forbidden in Plain English, so inner loops must be factored into separate routines.

Parameters and local variables are written as "a string" or "a length" (for example). These are subsequently referred to as "the string" and "the length." If you need to refer to more than one string, you could for example introduce "another string" and refer to it with "the other string." You may also name them whatever you want: "a random string" and refer to it either as "the random" or "the random string."

All arguments are passed by reference; even numbers. To output from a routine, you just modify the arguments. Then you simply refer to the object returned by the routine in the same way as you refer to parameters. For example, to use the routine defined above to output a random English word, you could:

To run:
Start up.
Make a random string given 5.
Write the random string on the console.
Wait for the escape key.
Shut down.

"To run:" is a special routine that signifies the starting point of the program. When added to the other three routines, this results in a program which will show something similar to the following when compiled and run:

Output of the program showing "JAWEF"

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I cant tell if this is a joke or not but I love it lol \$\endgroup\$ Dec 22, 2022 at 3:08
  • 1
    \$\begingroup\$ @MisterSirCode That was my first reaction too! But after reading Plain English's compiler, which is written in Plain English, I was quickly disabused of that notion! \$\endgroup\$
    – chunes
    Dec 22, 2022 at 3:33
3
\$\begingroup\$

Perl 5, 69 bytes

say map{([a,e,i,o,u],[grep!/[eiou]/,b..z])[$_%2][rand$_%2*16+5]}1..<>

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 89 bytes

f=(n,b)=>(b?"aeiou":"bcdfghjklmnpqrstvwyxz")[~~(Math.random()*(b?5:21))]+(--n?f(n,!b):"")

Try it online! (includes tests)

Takes n as length and b as whether to return vowel or consonant, default consonant.

Returns consonant or vowel based on b, decrements n and recurses with opposite b value.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 9 8 bytes

ØḄØẹḂ?X)

-1 byte thanks to @Steffan

Try it online!

Explanation:

ØḄØẹḂ?X)  # main link taking an integer

       )  # over the list [1..n]
     ?    # if
    Ḃ     # n modulo 2 is truthy
ØḄ        # yield the consonants
  Øẹ      # otherwise the vowels
      X   # get a random letter from that
```
\$\endgroup\$
3
  • 1
    \$\begingroup\$ ØḄØẹḂ?X) for 8 bytes by using (modulo 2) instead of checking for divisibility by 2. \$\endgroup\$
    – naffetS
    Oct 1, 2022 at 15:32
  • 1
    \$\begingroup\$ Another interesting 8-byter with ƭ (tie): ØḄØẹ2ƭX) \$\endgroup\$
    – naffetS
    Oct 1, 2022 at 15:34
  • \$\begingroup\$ @Steffan Thank you! Didn't know was a thing \$\endgroup\$ Oct 1, 2022 at 19:52
2
\$\begingroup\$

Stax, 10 bytes

ÇÅφ⌠↑Ñ°↕Yx

Run and debug it

Explanation

FVcVv2l@_@L

F           In range 1 to the input, do this ...
 Vv         Vowels
   Vc       Consonants
     2l     Wrap the two inside a list
       @    Index the constructed list into the counter
            Note that the counter starts at 1,
            so this picks the constants list
            before the vowels list.
        _@  Index by the current counter
            (Stax doesn't have random number support)
          L Wrap the whole stack into a list
\$\endgroup\$
1
  • \$\begingroup\$ Wow, nice job on this one \$\endgroup\$ Feb 13, 2020 at 12:17
2
\$\begingroup\$

Ruby, 76 bytes

Basic recursive function that adds a consonant and a vowel each time until there's only one character left, at which point it adds just the consonant.

f=->n{[*?b..?z].grep(/[^eiou]/).sample+(n<2?'':'aeiou'.chars.sample+f[n-2])}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

GolfScript, 36 bytes

,{['aeiou'.123,97>^]\2%=.,rand=}%''+

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save some bytes by using ['aeiou'.123,97>^] instead of your long string. Generates the characters 98-123, then removes the vowels. Plus, they're in two distinct arrays too, which saves you a bunch too. the string and 21/ can be removed, meaning you're down 31 chars and up 18, netting 13. ,{['aeiou'.123,97>^]\2%=.,rand=}%''+ This worked for me. \$\endgroup\$
    – Mathgeek
    Feb 14, 2020 at 14:18
2
\$\begingroup\$

Python, 83 bytes

lambda n:[*map(choice,["bcdfghjklmnpqrstvwxzy","aeiou"]*n)][:n]
from random import*

Attempt This Online!

Outputs a list of characters.

This is conceptually the same as grc's answer for "Generate a pronounceable word", since the questions are almost identical.

\$\endgroup\$
2
\$\begingroup\$

Vyxal s, 9 bytes

⟑₂¨ikvk¹℅

Try it online!

Explanation:

⟑          # Map over the range 1 to n
 ₂¨i       # If even:
    kv     #   Push the string "aeiou"
           # Otherwise:
      k¹   #   Push the lowercase consonants
        ℅  # Get a random character from the top of the stack
           # Concatenate the character list into a string with the s flag
\$\endgroup\$
2
\$\begingroup\$

Pip, 12 bytes

RC*[.CZVW]Ha

Try It Online!

Explanation

This should be 11 bytes; the . is a no-op to work around a bug in the current language version.

RC*[CZVW]Ha
   [    ]    List containing:
    CZ         Lowercase consonants
      VW       Lowercase vowels
          a  Command-line argument
         H   Take that many elements (cycling the list as necessary)
RC*          Random choice from each

I also found several 13-byte solutions:

RC{VW::CZ}M,a
RC[CZVW]@_M,a
LaORC(VW::CZ)
\$\endgroup\$
2
\$\begingroup\$

Bash, 93 89 bytes

v=aeiou]
until tr -dc a-z</dev/urandom|head -c$1|grep -E "^[^$v([$v[^$v)*[$v?$";do
:
done

Try it online!

A full command line program.

It generates random lowercase alpha strings of the required size in a loop until we find one matching the pattern ^<vowel>(<cons><vowel>)*<cons>?.

\$\endgroup\$
2
\$\begingroup\$

D, 124 bytes

auto f(int n){string s;for(import std;n;n--,s~=n%2?"aeiou"[uniform(0,5)]:"bcdfghjklmnpqrstvwxyz"[uniform(0,20)]){}return s;}

I'm not very good at code golf (but trying to get better) so this can probably be improved a lot

Try it online

\$\endgroup\$
6
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! Unfortunately we don't have a Tips for Golfing in D page, but it's nice to see someone using a language not regularly used on our site! \$\endgroup\$
    – pxeger
    Sep 2, 2022 at 8:35
  • \$\begingroup\$ You might want to add a Try it online link so others can test your code. Note: I don't know D and I'm probably doing this wrong. \$\endgroup\$
    – emanresu A
    Sep 2, 2022 at 9:43
  • \$\begingroup\$ Golfed off a few bytes. Idk what the issue with garbage bytes being appended is... \$\endgroup\$
    – emanresu A
    Sep 2, 2022 at 9:55
  • \$\begingroup\$ Unfortunately you can't rely on counting backwards since the vowels/consonants will swap depending on the parity of the input. You can fix this by changing s~=... to s=...~s. You can also combine the strings and indexing for 112 bytes \$\endgroup\$
    – Jo King
    Sep 2, 2022 at 11:58
  • \$\begingroup\$ the issue with garbage characters was since you were using cast(char*) to convert to a C string when passing to printf. you have to use toStringz (or the better solution is just to use the write/writeln function from std.stdio) \$\endgroup\$ Sep 4, 2022 at 5:48
2
\$\begingroup\$

Vyxal rṀs, 8 bytes

ƛkvk⁰"i℅

Try it Online!

Fun

\$\endgroup\$
1
\$\begingroup\$

Factor + pair-rocket sequences.repeating, 62 bytes

[ "bcdfghjklmnpqrstvwxyz"=> "aeiou"swap cycle [ random ] map ]

Attempt This Online!

                                   ! 5
"bcdfghjklmnpqrstvwxyz"=> "aeiou"  ! 5 { "bcdfghjklmnpqrstvwxyz" "aeiou" }
swap                               ! { "bcdfghjklmnpqrstvwxyz" "aeiou" } 5
cycle                              ! { "bcdfghjklmnpqrstvwxyz" "aeiou" "bcdfghjklmnpqrstvwxyz" "aeiou" "bcdfghjklmnpqrstvwxyz" }
[ random ] map                     ! "furom"
\$\endgroup\$
1
\$\begingroup\$

Pyth, 19 bytes

J"aeiou"smO?%d2J-GJ

Test suite

Explanation:
J"aeiou"smO?%d2J-GJ  | Full code
J"aeiou"smO?%d2J-GJQ | with implicit variables
---------------------+--------------------------------------------------------
J"aeiou"             | J = "aeiou"
         m         Q | For each d in [0...input):
          O          |  Pick a random letter from
               J     |   J
           ?%d2      |   if d is even else
                -GJ  |    the lowercase alphabet minus J (i.e. the consonants)
        s            | Print the concatenated results
\$\endgroup\$
1
\$\begingroup\$

Raku, 67 bytes

{$!=<a e i o u>;({(('a'..'z')∖$!,$!)[$++%2].pick}...*)[^$_].join}

Try it online!

  • The vowels are stored in $!, one of just two special global variables that don't need to be declared.
  • { ... } ... * is a lazy, infinite list, where the code between the braces generates each successive element.
  • ('a' .. 'z') ∖ $! are the consonants. That isn't a backslash, it's the Unicode SET MINUS character.
  • (('a' .. 'z') ∖ $!, $!)[$++ % 2] alternately chooses the consonants and vowels on each iteration of the generator. The anonymous state variable $ counts how many iterations there have been so far, and the % 2 reduces that to the alternating sequence 0, 1, 0, 1, ....
  • .pick chooses a random vowel or consonant.
  • [^$_] takes a number of elements from the sequence equal to the argument passed to the function.
  • .join joins those elements into a single string.
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 61 58 bytes

c;f(n){for(;n;4370%c-n&1&&putchar(c,n--))c=97+clock()%26;}

Try it online!

-3 bytes thanks to ceilingcat!!

\$\endgroup\$
0
1
\$\begingroup\$

Zsh --braceccl, 67 bytes

Try it Online!

shuf -n$1 <(echo {bcdfghj-np-tv-z}{aeiou}|rs 0 1)|rs -g0|cut -c -$1

Similar to the pronounceable word solution.

\$\endgroup\$
1
\$\begingroup\$

Japt -mP, 12 bytes

;Ck%+Ugciv¹ö

Try it

;Ck%+Ugciv¹ö     :Implicit map of each U in the range [0,input)
;C               :Lowercase alphabet
  k              :Remove
   %+            :  Append to "%"
     Ug          :  Index U into
       civ       :    "c" prepended with "v"
          ¹      :End remove ("%v"=/[aeiou]/gi "%c"=/[b-df-hj-np-tv-z]/gi)
           ö     :Random character
                 :Implicitly join & output
\$\endgroup\$
1
\$\begingroup\$

Nim, 148 bytes

import random
randomize()
proc x(y:int):string=
  let l="bcdfghjklmpqrstvwxyz";let a="auioe";for i in 0..y-1:result.add sample(if(i%%2)==0:l else:a)

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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