Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
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
3  
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

1 Answer 1

up vote 5 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:

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

Your Answer

 
discard

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.