Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
echo 1 | awk '{ srand(); print int (rand()*67) }'

I get a different o/p everytime I run it. However if I omit the 1 I get same o/p ..why?

share|improve this question
I can't reproduce this on OS X. With or without the 1 I get random output. – polynomial Oct 4 '11 at 3:10
you are right polynomial! – abc Oct 4 '11 at 3:30
The outcome of srand() varies once per second (it seeds the pseudorandom number generator based on the whole number of seconds since the origin of time), which explains why you might have seen the same output twice if you ran the command twice in quick succession. – Gilles Oct 4 '11 at 7:11
up vote 6 down vote accepted

It prints a random number between 1 and 67. It could also have been written without the echo:

awk 'BEGIN{srand(); print int(rand()*67+1)}'

See the Gnu Awk Users Guide:

Set the starting point, or seed, for generating random numbers to the value x.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.