random numbers in awk?

john.urban urban at cbnewsl.att.com
Wed Apr 24 23:37:11 AEST 1991


> article <26019 at adm.brl.mil>, d3e608%pnli.pnl.gov at pnlg.pnl.gov writes:
> I'm trying to write a csh script to extract a random multiline 
> record from a file.  Awk does a nice job of getting the records.  Now how

Several version of UNIX System V have nawk (new awk).  Regular awk does not
have the rand() function however nawk does.

To see if you version of UNIX has nawk, type in:

$ type nawk

If you've got nawk, then you can do something like:
	nawk 'BEGIN {print rand()}' < /dev/null
to get a random number on the screen

UNIX System V/386 Release 3.2 (and higher) also have the random(1) command
[Part of the XENIX System V and UNIX System V/386 merged product]

So you could use: random(1) although it doesn't appear to be total random.

I tried:
$ i=100
$ while [ $i -gt 0 ]
> do
> random 10
> i=`expr $i - 1`
> done | sort -n | uniq -c

  25 0
  10 4
  38 5
   3 6
  24 10


Sincerely,

John Urban



More information about the Comp.unix.questions mailing list