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.
-
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– Wheat Wizard ♦2020年02月07日 19:19:40 +00:00Commented 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– sugarfi2020年02月07日 19:49:46 +00:00Commented 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– Giuseppe2020年02月07日 20:46:21 +00:00Commented 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– Luis Mendo2020年02月07日 21:35:01 +00:00Commented 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– Giuseppe2020年02月12日 19:19:31 +00:00Commented Feb 12, 2020 at 19:19
21 Answers 21
-
\$\begingroup\$
¸ì
can beš
,.R
can beΩ
, and the}
can be removed before the join. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年02月10日 07:49:22 +00:00Commented Feb 10, 2020 at 7:49 -
\$\begingroup\$ oh wow, thank you! @KevinCruijssen \$\endgroup\$mabel– mabel2020年02月10日 09:07:19 +00:00Commented Feb 10, 2020 at 9:07
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:
-
1\$\begingroup\$ I cant tell if this is a joke or not but I love it lol \$\endgroup\$Mister SirCode– Mister SirCode2022年12月22日 03:08:03 +00:00Commented 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– chunes2022年12月22日 03:33:59 +00:00Commented Dec 22, 2022 at 3:33
Perl 5, 69 bytes
say map{([a,e,i,o,u],[grep!/[eiou]/,b..z])[$_%2][rand$_%2*16+5]}1..<>
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.
Jelly, (削除) 9 (削除ここまで) 8 bytes
ØḄØẹḂ?X)
-1 byte thanks to @Steffan
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
```
-
1\$\begingroup\$
ØḄØẹḂ?X)
for 8 bytes by usingḂ
(modulo 2) instead of checking for divisibility by 2. \$\endgroup\$naffetS– naffetS2022年10月01日 15:32:03 +00:00Commented Oct 1, 2022 at 15:32 -
1\$\begingroup\$ Another interesting 8-byter with
ƭ
(tie):ØḄØẹ2ƭX)
\$\endgroup\$naffetS– naffetS2022年10月01日 15:34:12 +00:00Commented Oct 1, 2022 at 15:34 -
\$\begingroup\$ @Steffan Thank you! Didn't know
Ḃ
was a thing \$\endgroup\$tybocopperkettle– tybocopperkettle2022年10月01日 19:52:11 +00:00Commented Oct 1, 2022 at 19:52
Stax, 10 bytes
ÇÅφ⌠↑Ѱ↕Yx
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
-
\$\begingroup\$ Wow, nice job on this one \$\endgroup\$Mister SirCode– Mister SirCode2020年02月13日 12:17:32 +00:00Commented Feb 13, 2020 at 12:17
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])}
-
\$\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– Mathgeek2020年02月14日 14:18:04 +00:00Commented Feb 14, 2020 at 14:18
Python, 83 bytes
lambda n:[*map(choice,["bcdfghjklmnpqrstvwxzy","aeiou"]*n)][:n]
from random import*
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.
Vyxal s
, 9 bytes
⟑2 ̈ikvk1c/o
Explanation:
⟑ # Map over the range 1 to n
2 ̈i # If even:
kv # Push the string "aeiou"
# Otherwise:
k1 # Push the lowercase consonants
c/o # Get a random character from the top of the stack
# Concatenate the character list into a string with the s flag
Pip, 12 bytes
RC*[.CZVW]Ha
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)
Bash, 93 89 bytes
v=aeiou]
until tr -dc a-z</dev/urandom|head -c1ドル|grep -E "^[^$v([$v[^$v)*[$v?$";do
:
done
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>?
.
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
-
\$\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– pxeger2022年09月02日 08:35:52 +00:00Commented 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– emanresu A2022年09月02日 09:43:18 +00:00Commented 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– emanresu A2022年09月02日 09:55:26 +00:00Commented 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~=...
tos=...~s
. You can also combine the strings and indexing for 112 bytes \$\endgroup\$Jo King– Jo King2022年09月02日 11:58:52 +00:00Commented 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\$The Zip Creator– The Zip Creator2022年09月04日 05:48:27 +00:00Commented Sep 4, 2022 at 5:48
Factor + pair-rocket sequences.repeating
, 62 bytes
[ "bcdfghjklmnpqrstvwxyz"=> "aeiou"swap cycle [ random ] map ]
! 5
"bcdfghjklmnpqrstvwxyz"=> "aeiou" ! 5 { "bcdfghjklmnpqrstvwxyz" "aeiou" }
swap ! { "bcdfghjklmnpqrstvwxyz" "aeiou" } 5
cycle ! { "bcdfghjklmnpqrstvwxyz" "aeiou" "bcdfghjklmnpqrstvwxyz" "aeiou" "bcdfghjklmnpqrstvwxyz" }
[ random ] map ! "furom"
Pyth, 19 bytes
J"aeiou"smO?%d2J-GJ
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
Raku, 67 bytes
{$!=<a e i o u>;({(('a'..'z')∖$!,$!)[$++%2].pick}...*)[^$_].join}
- 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 UnicodeSET 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 sequence0, 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.
C (gcc), (削除) 61 (削除ここまで) 58 bytes
c;f(n){for(;n;4370%c-n&1&&putchar(c,n--))c=97+clock()%26;}
-3 bytes thanks to ceilingcat!!
Zsh --braceccl
, 67 bytes
shuf -n1ドル <(echo {bcdfghj-np-tv-z}{aeiou}|rs 0 1)|rs -g0|cut -c -1ドル
Similar to the pronounceable word solution.
Japt -mP
, 12 bytes
;Ck%+Ugciv1ö
;Ck%+Ugciv1ö :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"
1 :End remove ("%v"=/[aeiou]/gi "%c"=/[b-df-hj-np-tv-z]/gi)
ö :Random character
:Implicitly join & output
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)
-
\$\begingroup\$ 119 bytes \$\endgroup\$ceilingcat– ceilingcat2025年04月14日 05:43:07 +00:00Commented Apr 14 at 5:43