in reply to First Run

The following depends heavily on the fact that certain operations, e.g.,  .. (range) and array indexing, are inherently integer operations. (BTW: The Windoze rand is not adequate for dealing with a large body of data like this; it's 15-bit IIRC!)

c:\@Work\Perl\monks>perl -wMstrict -le "chomp(my @words = <>); ;; for (1 .. 5 + rand 20) { my @line = map $words[ rand @words ], 0 .. rand 10; if ($#line) { $_ = ucfirst for $line[ 1 + rand $#line ] } print qq{\u@line.}; } " ..\..\moby\mwords\354984si.ngl Ouabaio soldiers loob Piedmontite phenylated nol. Zolle Torque ghbor. Oxygenicity Subpharyngal gastroscopic ventricular trainways incombinin +g indulgement avidin idiorepulsive. Understaff. Becrawls subclavicular Combustive. Wrestlers occluding cryptonema novelizations epexegesis legpulling def +icient publici phalangean Monkeyflower.

I think there's only one line that requires comment. TheloniusMonk's word corpus apparently includes capitalized and hyphenated words; mine doesn't. I'm not going to try to simulate hyphenated words, but I wanted, don't ask me why, to stick one capitalized word into each line to make my output look a little more like the OPed output. So the
    if ($#line) { $_ = ucfirst for $line[ 1 + rand $#line ] }
statement capitalizes a word other than the first in the line if the line has more than one word. (Update: The for statement modifier simply topicalizes the word randomly chosen from the array.) Whatever...

Update: I should have looked at Lotus1's Creating random sentences from a dictionary first. This post is essentially the same. Oh, well...


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: First Run (updated)
by Lotus1 (Vicar) on Sep 17, 2018 at 20:39 UTC
    I should have looked at Lotus1's Creating random sentences from a dictionary first. This post is essentially the same. Oh, well...

    It's the same approach but your solution is much more Perlish. I probably should have posted my solution in this thread. I liked how you did print qq{\u@line.}. I hadn't realized '\u' would only affect the first word since the array becomes a string. Also you can put the period at the end. This will be useful for me.

    In the line my @line = map $words[ rand @words ], 0 .. rand 10;I had already figured out that the range operator only returns integers so no need for int() on the rand 10 to feed into the map. But I didn't realize that 0 .. 0 would still give you one element for map so that was a very useful thing for me as well.

    (BTW: The Windoze rand is not adequate for dealing with a large body of data like this; it's 15-bit IIRC!)

    I found this stackoverflow article that says the value from rand(arg) is

    (arg * RAND) ------------- 2**randbits

    where RAND is a value from 0 to 2**randbits - 1. 'randbits' is a value that Perl is compiled with and can be found with the command perl -V:randbits. I found for 64 bit ActivePerl on my Windows 7 machine it is 15. On the same machine 32 bit Strawberry perl has randbits = 48. I've been trying to think of a way to test this but haven't arrived at anything.