O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The rand() function

The rand() function returns a random number x, between 0 and 1, such that 0 <= x <1. The value returned by the random function could be zero but it is never returned as zero. In AWK, rand() starts generating numbers from the same starting number (also known as the seed) on each run. The algorithm used to generate the random numbers is fixed, so the numbers are repeatable. Numbers are random within one AWK run, but they are predictable from run to run. To generate different numbers in each run, you must change the seed value for each run. This is generally done using srand(). The following code block shows an example of a rand(expr) function:

$ vi rand1.awkBEGIN    {    print "Random num1 = ", rand() print "Random num2 = ", rand() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required