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$
1
  • 4
    $\begingroup$ RandomWord? For instance, AbsoluteTiming[RandomWord["CommonWords", 100000];] takes only a fraction of a second. $\endgroup$
    – MarcoB
    Commented Dec 10, 2019 at 19:50

2 Answers 2

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"}]
$\endgroup$
2
  • 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$ Commented Dec 10, 2019 at 19:55
  • $\begingroup$ @ClydeTheGhost Glad it helped! $\endgroup$
    – MarcoB
    Commented Dec 10, 2019 at 19:55
3
$\begingroup$

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

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

Your Answer

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

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