Numeric Functions (The GNU Awk User’s Guide)
Next: String Functions, Previous: Calling Built-in, Up: Built-in [Contents][Index]
9.1.2 Numeric Functions
The following list describes all of the built-in functions that work with numbers. Optional parameters are enclosed in square brackets ([ ]): /@w
atan2(y, x)Return the arctangent of
y / xin radians. You can use ‘pi = atan2(0, -1)’ to retrieve the value of pi.cos(x)Return the cosine of
x, withxin radians.exp(x)Return the exponential of
x(e ^ x) or report an error ifxis out of range. The range of valuesxcan have depends on your machine’s floating-point representation.int(x)Return the nearest integer to
x, located betweenxand zero and truncated toward zero. For example,int(3)is 3,int(3.9)is 3,int(-3.9)is -3, andint(-3)is -3 as well.log(x)Return the natural logarithm of
x, ifxis positive; otherwise, returnNaN(“not a number”) on IEEE 754 systems. Additionally,gawkprints a warning message whenxis negative.rand()Return a random number. The values of
rand()are uniformly distributed between zero and one. The value could be zero but is never one.44Often random integers are needed instead. Following is a user-defined function that can be used to obtain a random nonnegative integer less than
n:function randint(n) { return int(n * rand()) }The multiplication produces a random number greater than or equal to zero and less than
n. Usingint(), this result is made into an integer between zero andn- 1, inclusive.The following example uses a similar function to produce random integers between one and
n. This program prints a new random number for each input record:# Function to roll a simulated die. function roll(n) { return 1 + int(rand() * n) } # Roll 3 six-sided dice and # print total number of points. { printf("%d points\n", roll(6) + roll(6) + roll(6)) }CAUTION: In most
awkimplementations, includinggawk,rand()starts generating numbers from the same starting number, or seed, each time you runawk.45 Thus, a program generates the same results each time you run it. The numbers are random within oneawkrun but predictable from run to run. This is convenient for debugging, but if you want a program to do different things each time it is used, you must change the seed to a value that is different in each run. To do this, usesrand().sin(x)Return the sine of
x, withxin radians.sqrt(x)Return the positive square root of
x.gawkprints a warning message ifxis negative. Thus,sqrt(4)is 2.srand([x])Set the starting point, or seed, for generating random numbers to the value
x.Each seed value leads to a particular sequence of random numbers.46 Thus, if the seed is set to the same value a second time, the same sequence of random numbers is produced again.
CAUTION: Different
awkimplementations use different random-number generators internally. Don’t expect the sameawkprogram to produce the same series of random numbers when executed by different versions ofawk.If the argument
xis omitted, as in ‘srand()’, then the current date and time of day are used for a seed. This is the way to get random numbers that are truly unpredictable.The return value of
srand()is the previous seed. This makes it easy to keep track of the seeds in case you need to consistently reproduce sequences of random numbers.POSIX does not specify the initial seed; it differs among
awkimplementations.
Footnotes
(44)
The C version of rand() on many Unix systems is known to produce fairly poor sequences of random numbers. However, nothing requires that an awk implementation use the C rand() to implement the awk version of rand(). In fact, for many years, gawk used the BSD random() function, which is considerably better than rand(), to produce random numbers. From version 4.1.4, courtesy of Nelson H.F. Beebe, gawk uses the Bayes-Durham shuffle buffer algorithm which considerably extends the period of the random number generator, and eliminates short-range and long-range correlations that might exist in the original generator.
(45)
mawk uses a different seed each time.
(46)
Computer-generated random numbers really are not truly random. They are technically known as pseudorandom. This means that although the numbers in a sequence appear to be random, you can in fact generate the same sequence of random numbers over and over again.
Next: String Functions, Previous: Calling Built-in, Up: Built-in [Contents][Index]