Formatting A Program Using Randomization

IT-SC 141 string concatenation operator. These have been placed at the beginning of each line to clarify the overall structure of the statement.

7.2.4 Randomly Selecting an Element of an Array

Lets look closely at one of the sentence part selectors: verbs[intrandscalar verbs] These kinds of nested braces need to be read and evaluated from the inside out. So the expression thats most deeply surrounded by braces is: scalar verbs You see from the comments before the statement that the built-in function scalar returns the number of elements in an array. The array in question, verbs , has seven elements, so this expression returns 7. So now you have: verbs[intrand7] and the most deeply nested expression is now: rand7 The helpful comments in the code before the statement remind you that this statement returns a pseudorandom number greater than 0 and less than 7. This number is a floating-point number decimal number with a fraction. Recall that an array with seven elements will number them from 0 to 6. So now you have something like this: verbs[int3.47429] and you want to evaluate the expression: int3.47429 The int function discards the fractional part of a floating-point number and returns just the integer part, in this case 3. So youve come to the final step: verbs[3] which gives you the fourth element of the verb s array, as the comments have been kind enough to remind you.

7.2.5 Formatting

IT-SC 142 To randomly select a verb, you call a few functions: scalar Determines the size of the array rand Picks a random number in the range determined by the size of the array int Transforms the floating-point number rand returns into the integer value you need for an array element Several of these function calls are combined in one line using nested braces. Sometimes this produces hard-to-read code, and the gentle reader may be nodding his or her head vigorously at this unflattering characterization of the authors painstaking handiwork. You could try rewriting these lines, using additional temporary variables. For instance, you can say: verb_array_size = scalar verbs; random_floating_point = rand verb_array_size ; random_integer = int random_floating_point; verb = verbs[random_integer]; and repeat for the other parts of speech, finally building your sentence with a statement such as: sentence = subject verb object prepositional_phrase. ; Its a matter of style. You will make these kinds of choices all the time as you program. The choice of layout in Example 7-1 was based on a tradeoff between a desire to express the overall task clearly which won balanced against the difficulty of reading highly nested function calls which lost. Another reason for this layout choice is that, in the programs that follow, youll select random elements in arrays with some regularity, so youll get used to seeing this particular nesting of calls. In fact, perhaps you should make a little subroutine out of this kind of call if you will do the same thing many times? Readability is the most important thing here, as it is in most code. You have to be able to read and understand code, your own as well as the code of others, and that is usually more important than trying to achieve other laudable goals such as fastest speed, smallest amount of memory used, or shortest program. Its not always important, but usually its best to write for readability first, then go back and try to goose up the speed or whatever if necessary. You can even leave the more readable code in there as comments, so whoever has to read the code can still get a clear idea of the program and how you went about improving the speed or whatever.

7.2.6 Another Way to Calculate the Random Position