Previous | Table of Contents | Next |
5.2.7.5. The Pseudo-Random Number Generator
In addition, a pseudo-random number generator (PRNG) exists; it qualifies as a mathematical function. It is referred to as pseudo-random and not a truly random number generator, as it uses an algebraic equation with a seed and a given range in order to generate a number sequence that seems random.
When the seed is given to be constant in the PRNG, and consequently the program itself, it generates the same constant stream of numbers upon each execution of the program containing the PRNG statements. But a method can be used to randomize the seed from the system clock to make the number sequences appear to be random.
The seed for the PRNG is held in a longint variable called randseed, which may be set directly, if a repeating constant stream of numbers is desired. Otherwise, a procedure named randomize may be used in order to randomize the variable randseed in memory. Then to generate one number of the series, a function named random() may be used. Without parameters, it generates a real number between 0 and 1. With a parameter, it generates a number between 0 and (parameter 1). For example, random(100) generates an integer between 0 and 99. A sample use of the PRNG is shown in Listing 5.29.
Listing 5.29. A demonstration of the PRNG.
program fig29; { demonstration of the PRNG, distribution of 100 pseudo-randomly generated numbers between 1 and 10count of occurrences } var pcount: array[1..10] of byte; i: byte; begin randomize; { call this only *once* at the beginning of the program } for i := 1 to 10 do pcount[i] := 0; for i := 1 to 100 do inc(pcount[random(10)+1]); for i := 1 to 10 do writeln(i, occurred , pcount[i], times.); end.
5.2.7.6. Bit Manipulation
This section describes the bit operators and functions available under Turbo Pascal. As is known, a byte is made up of 8 bits, each designated either 1 or 0. The manipulation of bits involves some bit shifting or boolean algebra in any of the 1-, 2-, or 4-byte numerical variables. One-byte examples are used in this section.
Generally, when were talking about bits, the most significant bits are to the left, as this is how most of us are used to dealing with numbers in algebra. Computer systems, though, do not necessarily deal with items in this way. Some are big endian, meaning they deal with bits in the exact manner humans are used to dealing with them. Others, like PCs, are little endian, meaning the least significant bit is first, when reading from left to right, in order to the most significant bit. Note that with Turbo Pascal, you will be dealing with a little endian system if you use a hex editor for observing storage methods.
Shifting Bits
In shifting bits, they can either be shifted left or right. The corresponding operators to do that in Turbo Pascal are shl, or shift left, and shr, or shift right. In shifting, the bits are physically moved, with 0s assumed on the remaining portions. If any relevant bytes are pushed out of the datas range for storage, they are lost. Note, in the following examples, that within storable limits, shifting bits can be equivalent to multiplying or integer division by the power of 2 the bits are shifted by in the operation.
Each operator is used by placing the value to be operated on, followed by shl or shr, and the number of places to be shifted.
Figure 5.6. A demonstration of the effects of bit shifting (shl and shr).
Boolean Algebra
In manipulating bits, boolean algebra or logic can be used to work with variables on a bit level. Types of boolean algebra described are AND, OR (inclusive OR), or XOR (exclusive OR). Before we continue the description, see the listing of boolean algebra rules in terms of bits. The following AND and OR rules are exactly like those encountered in control statements, when 1 is true and 0 is false:
In the case of multiple bits, each bit is evaluated depending on the rules as described here. Examples of the bit operations are shown in Figure 5.7, and appear in a TP program listing in a similar manner in decimal form.
Figure 5.7. A sample of bitwise boolean algebra operators.
In using the boolean operators, the patterns of the answers can reveal the use of these operators. AND can be used quite well to test the existence of a bit or bits at specific positions. Note in the example for AND that the bytes which remain 1s or true are the ones that appear in both columns. With the OR example, any bits that are true and dissimilar are forced to true. In the XOR example, it could that in the positions the 1s are in the second object of the operator, the bits were toggled from 1 to 0, or 0 to 1. A coded example of the boolean bit operators and bit shifting are shown in Listing 5.30.
Previous | Table of Contents | Next |