# Quickly generate random text

I am writing some tests and would like to introduce large chunks of random alphabetic text (just characters a-z) to the input. The way I am generating the text now is like this:

RandomString[length_Integer] :=
StringJoin[
Table[
FromLetterNumber[RandomInteger[{1, 26}]],
length
]
]


This works great but is slow: AbsoluteTiming[RandomString[100000]] shows it running in 6.69763 seconds, which is too slow to run on lots of tests.

Does anyone know of a faster way to generate random text?

• RandomWord? For instance, AbsoluteTiming[RandomWord["CommonWords", 100000];] takes only a fraction of a second. Dec 10 '19 at 19:50

The following will give you a long string of text using common words separated by a space; it is very fast after the first execution (which loads some indices):

StringRiffle@RandomWord["CommonWords", 100]


"dexterous calibration ethical nocturnal misfortune ruining commodious refreshing gable arithmetic sacristy doorknocker thread measles pittance disrupted chorister discharge arbitrarily midday vainglory anvil walking spotty philosopher plutonium balboa lynx resignedly distinct python photostat platypus okra predetermination fanlight knickknack scuttlebutt adios silents forewarn business carbonic pigeonhole motivation diagnostician awareness flushed sentimentally painting shirring impeccable proficiency racketeering snack soulfully excision chaplain writer anthologist psychologically fulfillment televangelist Thursday gibbon broadcasting hideously recombination voucher brownstone absentee blockbuster freshly unpasteurized freethinker womanliness gender bellwether overrating pronoun tarmacadam reachable hypothermia elsewhere knead egress exonerate nuts slanted hand-to-hand pyramidal visualize characterize thriftless strap quite tourney baryon alarm consanguineous"

Alternatively, you can also use the built in Lorem Ipsum in the example data collection:

ExampleData[{"Text", "LoremIpsum"}]

• This is great! Of course there are so many functions I don't know about in Mathematica, RandomWord is new to me but definitely fits the bill. Dec 10 '19 at 19:55
• @ClydeTheGhost Glad it helped! Dec 10 '19 at 19:55

just in case someone is not interested in real words and wants something faster

StringJoin@RandomChoice[Flatten@{Alphabet[], " "}, 100000]

• This is an interesting solution that doesn't involve loading a bunch of dictionary words at the beginning. But I think you made a slight typo: did you mean: StringJoin@RandomChoice[Append[Alphabet[], " "], 100000] ? May 19 '20 at 20:13
• @ClydeTheGhost ah yes thanks for pointing it out, i forgot to flatten the list May 19 '20 at 20:15