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] :=
            FromLetterNumber[RandomInteger[{1, 26}]],

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?

  • 2
    $\begingroup$ RandomWord? For instance, AbsoluteTiming[RandomWord["CommonWords", 100000];] takes only a fraction of a second. $\endgroup$ – MarcoB 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"}]
|improve this answer|||||
  • $\begingroup$ 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. $\endgroup$ – ClydeTheGhost Dec 10 '19 at 19:55
  • $\begingroup$ @ClydeTheGhost Glad it helped! $\endgroup$ – MarcoB Dec 10 '19 at 19:55

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.