When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Sunday, May 28, 2000

Generating Random "Fake Latin" Strings

By Dave Cantrell

During a recent meeting I presented some initial design comps for a planned enterprise-wide web site re-design. The focus at this point was on information architecture, and the designs were in the initial draft stage, only to be used to give the higher management an idea of the scope and impact of these changes. It wasn't long before the designs were criticized by several people, not for their design, but for the content -- I had cut & pasted actual content from the existing web sites with the intent to help provide a better mental image. However, they took it upon themselves (after being told otherwise, no less) to point out the factual and grammatical errors in the text. Rankled, I decided not to encounter this problem anymore.

- continued -

I remembered Jakob Nielsen's Alertbox article on using "greeked" text to test designs, and decided to put it to good use. I had been on the lookout for an app that would generate this text for me but had no luck. Then I found some sample munged-up text and proceeded to write a few functions to handle this for me. Bear in mind this is ASP code, but should be easily portable to a VB app or client-side DHTML.

The two functions are GetLatinSentence and GetLatinParas. GetLatinSentence accepts an integer and returns a fully-formed sentence containing the specified number of random words, with the first word capitalized and commas randomly spaced throughout. GetLatinParas accepts an integer and returns the specified number of paragraphs, each containing a random number of sentences generated from GetLatinSentence. A new random number is passed in each time GetLatinSentence is called.

On to the code...

This is the larger of the two functions. First, we declare the function as normal and then localize the input variables (always a good practice! eliminates changing the input value and losing track of it!):

Function GetLatinSentence( cntWords_IN )
  dim cntWords
  cntWords = cntWords_IN

Next we set up the array of words we will use for the sentence, and declare our output variable.

  dim strLatin    'the base string we build the array from
  dim arrLatin    'array of the words we will use
  strLatin = LCase( "lorem ipsum dolor sit amet consectetuer adipiscing elit " & _
                    "sed diam nonummy nibh euismod tincidunt ut laoreet dolore " & _
                    "magna aliquam erat volutpat ut wisi enim ad minim veniam " & _
                    "quis nostrud exerci tation ullamcorper suscipit lobortis nisl " & _
                    "ut aliquip ex ea commodo consequat duis autem vel eum iriure " & _
                    "dolor in hendrerit in vulputate velit esse molestie consequat " & _
                    "vel illum dolore eu feugiat nulla facilisis at vero eros et " & _
                    "accumsan et iusto odio dignissim qui blandit praesent luptatum " & _
                    "zzril delenit augue duis dolore te feugait nulla facilisi Ut " & _
                    "wisi enim ad minim veniam quis nostrud exerci tation " & _
                    "ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo " & _
                    "consequat duis autem vel eum iriure dolor in hendrerit in " & _
                    "vulputate velit esse molestie consequat vel illum dolore eu " & _
                    "feugiat nulla facilisis at vero eros et accumsan et iusto " & _
                    "odio dignissim qui blandit praesent luptatum zzril delenit " & _
                    "augue duis dolore te feugait nulla facilisi" )
  arrLatin = Split( strLatin, " " )
  'this will be our output
  dim strResult
  strResult = ""

Now we start building the output. First we do Randomize to insure we get a random seed value. I used the UBound value of the array in the iRandom calculation because we use iRandom to reference the array's ordinal positions.

  'start building the output
  dim iRandom   'our random number
  iRandom = Int( UBound( arrLatin ) * Rnd )

Next we start the sentence by pulling one word out of the array at random and and converting the first letter to uppercase. I used the most excellent PCase function from line9.com for this (link is in the source code).

   strResult = strResult & PCase( arrLatin( iRandom ) )

blnComma is a flag the script will set randomly and use to see if we should insert a comma after each word.

   dim blnComma  'flag to see if we place a comma

Now we start the loop. We reset the iRandom variable because we don't want the same word to come out twice in a row. It still might with the way the script is currently written, but at least it's less probable.

  For i = 1 to cntWords
    'build a random number between 0 and the upper limit of the string
    iRandom = Int( UBound( arrLatin ) * Rnd )

Next is a check to see if we should put a comma after this word. Since we don't want a comma every second or third word, we set this to a one-in-ten chance. (BTW, since we add the comma to strResult before adding the next word, we shouldn't get any commas followed by periods)

    blnComma = Int( 8 * Rnd )
    If blnComma = 1 then
      'yes we include a comma
      strResult = strResult & ","
    End If

Now we concatenate the string by adding a space and the selected word.

    strResult = strResult & " "
    strResult = strResult & arrLatin( iRandom )

Finally, we add a period to the end of the sentence, return the value, and end the function:

  strResult = strResult & ". "
  'return the result
  GetLatinSentence = strResult
End Function

In Part 2 we'll look at the code for the GetLatinParas function, which generates fake Latin paragraphs!

  • Read Part 2

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article