10/17/2014

Using Python random functions to create new words

The Python programming language lets you randomly select syllables and then concatenate (combine) them to generate new words. This document explains how to control syllable concatenation through Python random functions.

Python is free to download, install, and use. It is easy and fun to program. Before trying the examples in this document, you need to install Python 3 on your computer. For more information, see Getting started with Python programming.

Using Python random functions

Python random functions generate random values that you can use directly or for other purposes, such as selecting list items randomly. For more information, about random functions, see see Wikibooks: Python Random Numbers. For really geeky details, see Python Library: Random Numbers.

Figure 1 shows how you can use a few fundamental Python random functions. If you want try this code, you can either type it from Figure 1 or copy it from its NwWrdsRdmBasics online example.

Figure 1 - This example Python code demonstrates a few random-function 
capabilities, such as basing random values on the computer clock, 
returning a random floating point value, returning a random integer value,
 and returning a random item from a list.
In Figure 1:
  • The 'import random' statement imports the Python random module so that you can use random functions in this example code.
  • The random.seed() function bases all random functions on the computer clock.
  • Each print() function displays the number or string returned by its random function.
  • The random.random() function returns a random floating point (decimal) value, 00 through 1.0.
  • Each random.randint(a,b) function returns a random integer (whole number) value, in the range a through b.
  • The random.choice(y) function randomly selects and returns one item from list y.
Figure 2 demonstrates how you can use two Python random functions to create a large random decimal value. If you want try this code, you can either type it from Figure 2 or copy it from its NewWrdsRdmLgFloat online example.

Figure 2 - This example Python code demonstrates
creating a large random decimal value.
In Figure 2:
  • The 'a = random.random()' statement assigns a floating point value, 0.0 through 1.0, to variable a.
  • The 'b = random.randint(0,999)' statement assigns an integer value, 0 through 999, to variable b.
  • The 'c = a + b' statement assigns the sum of a and b to variable c.
  • The print (c) function displays a large random decimal value, such as 467.33257201.

Concatenating Strings in Python

A string is a variable that consists of one or more characters. Python lets you concatenate strings by placing them in sequence with no spaces among them. In Python, plus signs let you either add numerical values or concatenate strings.

Figure 3 demonstrates adding numerical values and concatenating strings.

Figure 3 - This example Python code demonstrates
using plus signs to either add values or concatenate strings

In Figure 3:
  • The 'd = a + b + c' statement assigns the sum of a, b, and c to variable d. Variables a, b, and c must be numerical values; for addition, none can be strings.
  • The 'h = e + f + g' statement assigns concatenated strings e, f, and g to variable h. Variables e, f, and g must be strings; for concatenation, none can be numerical values.

Creating New Words in Python

To create a new word in Python, you need to randomly choose syllable strings from a list, and then concatenate those syllables.

Note: Beyond demonstrating Pythom random functions and concatenation, creating new words can be useful. For example, if you write science fiction, you can use a Python program to create new space-alien expletives, such as "tulktokloogkek," "garfblugtonk," and "loogblug." However, you need not write science fiction. For example, if you are expecting a baby, why not give him or her an original name, such as "Depgarf?"

Figure 4 shows how you can use Python random functions and concatenation to create a two-, three-, four-, or five-syllable new, random word.

Figure 4 - This example Python code demonstrates
randomly selecting syllables from a single list of strings,
and then concatenating them to create a new word.
Syllables can repeat sequentially
In Figure 4:
  • The 'w = random.randint(2,5)' statement assigns an integer value, 2 through 5, to variable w. This determines whether the new word contains two, three, four, or five syllables.
  • The 'c = random.choice(y)' statement assigns one random syllable string, from list y, to variable c. Likewise, the 'd = random.choice(y)' statement assigns one random syllable to variable d. Note: Variables c and d apply to words that have two or more syllables. Variable e applies to words that have three, four or five syllables.  Variable f applies to words that have four or five syllables. Variable g applies to words that have five syllables.
  • The 'if w == 2:' control flow statement executes the 'print (c + d)' function, which concatenates and displays a two-syllable word, only if w is equal to 2.
  • The 'elif w == 3:' control flow statement executes the 'e = random.choice(y)' statement and the 'print (c + d +e)' function, which concatenates and displays a three-syllable word, only if w is equal to 3.
  • The 'elif w == 4:' control flow statement executes the 'e = random.choice(y)' statement, the 'f = random.choice(y)' statement, and the 'print (c + d +e +f)' function, which concatenates and displays a four-syllable word, only if w is equal to 4.
  • The 'elif w == 5:' control flow statement executes the 'e = random.choice(y)' statement, the 'f = random.choice(y)' statement, the 'g = random.choice(y)' statement, and the 'print (c + d +e +f +g)' function, which concatenates and displays a five-syllable word, only if w is equal to 5. Note: If you wish, you can edit the syllable strings in list y. You can also add or delete them.
Figure 5 shows how you can modify the example code in Figure 4 so that it prevents sequential duplicate syllables. (After all, many kids might ridicule one named 'zotzot.')

Figure 5 - This example Python code demonstrates
randomly selecting syllables from two lists of strings,
and then alternately concatenating the strings to create a new word.
Syllables cannot repeat sequentially.
In Figure 5:
  • The 'x = random.randint(0,1)' statement assigns either 0 or 1 to variable x.
  • Lists y and z each contain half the syllable strings.
  • The if and else control flow statements determine which list provides the first syllable. If x is 0, list a is a switched copy of list y, and list b is a copy of list z. If x is 1, list a is a copy of list z, and list b is a copy of list y.
  • The remaining code in Figure 5 is similar to that in the Figure 4.

No comments :

Post a Comment