9. Random Phrase Generator

Creativity block is crippling.  You can sit in front of a blank screen for hours, invariably you end up browsing the web or hopping from YouTube clip to YouTube clip. Some time ago I read a suggestion that if you open a dictionary at a random page then, with your eyes closed, choose a word on the page  you could use that as a starting point or trigger for whatever you were doing. For instance, you are interested in writing a book about writer's block.  Using the dictionary method you choose the word "idol" (this was genuinely picked randomly).  Thinking about this word you may think of who your writing idol's are and how they solved their writer's block problem.  And you are off.

What has that to do with FreeBASIC programming?  Well I thought we can do better than a random dictionary word.  How about a whole phrase made up of a verb, and adjective, and a noun. First off we need our list of words.  I found the 100 most frequently used English verbs in  (the original source is but you have to play around with the interface a bit.)   The same web site will give you the adjectives and nouns.  I have done the search for you so you don't have to; keep the thanks until later :-).

The following program can be typed into your IDE or simply cut and paste.

'module for returning a
'verb adjective noun phrase

 Dim asVerb(1 to 100) as String
 Dim asAdjective(1 to 100) as String
 Dim asNoun(1 to 100) as String

 Dim sInputString as String = ""
 Dim iLocation as Integer = 0

 Dim iIndex as Integer = 0

 'Fill asVerb array
 sInputString = "be have do say go can get would make know will think take see" _
     + " come could want look use find give tell work may should call try ask" _
     + " need feel become leave put mean keep let begin seem help talk turn start" _
     + " might show hear play run move like live believe hold bring happen must" _
     + " write provide sit stand lose pay meet include continue set learn change" _
     + " lead understand watch follow stop create speak read allow add spend grow" _
     + " open walk win offer remember love consider appear buy wait serve die send" _
     + " expect build stay fall cut reach kill remain "

 for iIndex = 1 to 100
     iLocation = Instr(sInputString, " ")
     asVerb(iIndex) = Left(sInputString, iLocation)    
     sInputString = Mid(sInputString, iLocation + 1)

 'Fill asAdjective array
 sInputString = "other new good high old great big American small large" _
     + " national young different black long little important political" _
     + " bad white real best right social only public sure low early" _
     + " able human local late hard major better economic strong possible whole" _
     + " free military true federal international full special easy clear recent" _
     + " certain personal open red difficult available likely short single medical" _
     + " current wrong private past foreign fine common poor natural significant" _
     + " similar hot dead central happy serious ready simple left physical general" _
     + " environmental financial blue democratic dark various entire close legal" _
     + " religious cold final main green nice huge popular traditional cultural "
 for iIndex = 1 to 100
     iLocation = Instr(sInputString, " ")
     asAdjective(iIndex) = Left(sInputString, iLocation)
 '    print asAdjective(iIndex)
     sInputString = Mid(sInputString, iLocation + 1)

 'Fill asNoun array
 sInputString = "time year people way day man thing woman life child world school" _
     + " state family student group country problem hand part place case week" _
     + " company system program question work government number night point home" _
     + " water room mother area money story fact month lot right study book eye" _
     + " job word business issue side kind head house service friend father power" _
     + " hour game line end member law car city community name president team minute" _
     + " idea kid body information back parent face others level office door health" _
     + " person art war history party result change morning reason research girl guy" _
     + " moment air teacher force education "

 for iIndex = 1 to 100
     iLocation = Instr(sInputString, " ")
     asNoun(iIndex) = Left(sInputString, iLocation)

     sInputString = Mid(sInputString, iLocation + 1)

'print out a phrase made up of a random: verb + adverb + noun

 for iIndex = 1 to 10
     Print asVerb(int(Rnd * 100) + 1) + " " + asAdjective(int(Rnd * 100) + 1) _
         + " " + asNoun(int(Rnd * 100) + 1)


Let's go through the program to see how it works.  The first section is for allocating variables.  We have set up three arrays of 100 strings each, one each for our verb, our adjective, and our noun.  The other variable sInputString will hold a string made up of each separate word separated by spaces. The iLocation variable will be used to find the spaces separating the words within the sInputString. And the iIndex variable will be used for working through our arrays.

Three similar sections follow.  Let's work through the first one, the one with the Fill asVerb array comment. The first part of this block assigns the separate verbs into the sInputString variable. To do this I have put enough words to fill a line then added the underscore character ( _ ) at the end. This character tells FreeBASIC that the line is not finished but continues.  The start of the next line starts with a plus character ( + ).  This character concatenates the following string with the previous string.

We now have sInputString made up of the 100 most common verbs separated by spaces.  The next bit of code needs some explanation.

 for iIndex = 1 to 100
     iLocation = Instr(sInputString, " ")
     asVerb(iIndex) = Left(sInputString, iLocation)    
     sInputString = Mid(sInputString, iLocation + 1)

The first line will loop the block from 1 to 100 and keep the iIndex variable updated.  The second line assigns the location of the first space, " ", in the string to the iLocation variable.  The Instr() function is built into FreeBASIC and takes the string to be searched (sInputString in this case) and the string we are looking for ( " " - ie the space character) as its parameters. It returns the numerical position of the first instance of the found string.

The next line then uses the inbuilt Left() function assigns the string to the left of iLocation, the location of the first space, the variable asVerb(iIndex).  On the first pass it will allocate the first verb "be" to asVerb(1).

The next line takes our sInputString and assigns it a new string made up of the original string but starting from the second word.  The Mid() function takes a source string, sInputString in this case, and a location within that string and returns a new string made up of the source from the location to the end of the string.  In our case the location is the position following the space after "be".  So sInputString will now be the same as before however we have dropped off the first word and space.  It now begins with "have".

The process is looped 100 times so sInputString progressively drops off the first word and space and commences with a new word.  That new word is then assigned the asVerb array before it too is dropped off.

The adjectives and nouns are handled in exactly the same manner and so we end up with three arrays: one for the verbs, one for the adjectives, and one for the nouns. In a previous version I had this part of the program read the words from three separate text files.  I have opted to show self contained version here but feel free to try that way for yourself.  The benefit for the three file version is that you can easily change any of the words with text editor without having to recompile the program.  The benefit of what I have shown above is that the program is self-contained and probably runs faster since it doesn't have to read external files.

The last block of the program prints out 10 phrases made up of random verb-adjective-noun combinations.  If you want some more practice you can change the number of phrases produced or send the phrases to a text file so you can print them out at your leisure.  You can try this idea with other language parts. Or even different languages.

That's all there is to it.  Not too difficult but I find the phrases very useful for coming up with creative ideas.  Let me know if you use this routine what you use it for.

As always, please feel free to leave a comment and see you in the next post.


  1. Very nice example. Programming is fun. It's like painting a picture. You start with a fresh canvas and ponder the idea; what shall I paint today.

    If you can, it would be nice to have a recap at the bottom of each blog post, pointing out the Freebasic commands used and links to those commands in Freebasic's wiki so that new programmers will know how and where to find the Freebasic commands and there usage.

  2. I drive a truck for a living. Last year I was working for an owner operator leased on to fedex custom critical. I put a photo on of myself with the truck in the back ground. The job requires team drivers as most loads need to be delivered yesterday while maintaining security for the customer's products.

    In my spare time working this job, I wrote fbcadcam-macro. I also had the opportunity to drive team with the owner operator for a couple weeks. Bogdan Pastor is terrific and I would recommend others to work for him. In particular though, Bogdan permitted me to talk about programming and prime numbers for a few minutes or so which was awfully nice for me in my solitude.

    Searching for an idea of what kind of program I could make in order to attempt to share the joy of what it means to write a computer program, I thought about what Bogdan would be interested in. After having breakfast at Cracker Barrel, I suggested the idea that I would demonstrate how to make a program of the Jump-A-Peg game that we were playing at the restaurant. Below I will include the code for the game I made for my boss but I have to tell you, I was really disappointed when he was only interested in playing the game and not learning how to code. But, you know, I'll take what I can get and try to be happy with that.

    'the triangle game
    Dim As Integer i,j,k,x,y,c,peg,pegup,pegpickedup,pass,pegout,moves(30,2),movesc,start
    Dim As Integer hole(15,3)
    Dim As String h,gamename

    Randomize Timer

    this blog comment is limmited to 4000 characters or so. I'm not able to post it all here. I'll put it on fbcadcam's server if anyone is interested.

  3. here is the link to the code for the jump-a-peg game