3
$\begingroup$

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?

$\endgroup$
  • 4
    $\begingroup$ RandomWord? For instance, AbsoluteTiming[RandomWord["CommonWords", 100000];] takes only a fraction of a second. $\endgroup$ – MarcoB Dec 10 '19 at 19:50
8
$\begingroup$

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 | |
$\endgroup$
  • 1
    $\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
3
$\begingroup$

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

StringJoin@RandomChoice[Flatten@{Alphabet[], " "}, 100000]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ 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] ? $\endgroup$ – ClydeTheGhost May 19 at 20:13
  • $\begingroup$ @ClydeTheGhost ah yes thanks for pointing it out, i forgot to flatten the list $\endgroup$ – Alucard May 19 at 20:15

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.