For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2007 > rand() function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author rand() function
Doug McClure

2007-02-16, 3:57 am

The Awk Programming Language book by Aho, Weinberger, and Kernighan
states that the values returned from the rand() function are [0.0,
1.0). That is -- 0 is a possible value but 1.0 is not.

I use Thompson AWK, TAWK, but usually refer to the GAWK Windows Help
File when I have a question, and the GAWK Help file indicates that the
range is (0.0, 1.0). This differs from the AWK Programming Language
book in that 0.0 is not possible.

A search with Yahoo of "rand awk" shows a lot of webpages that aren't
at all specific about the endpoints.

Does anyone know why GAWK defined rand() differently? And are there
any gotchas that need to be accomodated when writing AWK programs that
might run under TAWK or might run under GAWK.

Thanks,

DKM


To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Janis Papanagnou

2007-02-16, 6:57 pm

Doug McClure wrote:
> The Awk Programming Language book by Aho, Weinberger, and Kernighan
> states that the values returned from the rand() function are [0.0,
> 1.0). That is -- 0 is a possible value but 1.0 is not.


Isn't that equivalent to what the gawk "User's Guide" (Ed.3/June 2004)
says?

>
> I use Thompson AWK, TAWK, but usually refer to the GAWK Windows Help
> File when I have a question, and the GAWK Help file indicates that the
> range is (0.0, 1.0). This differs from the AWK Programming Language
> book in that 0.0 is not possible.


Hmm.. - "The value could be zero but is never one." [from the above doc]
seems not to exclude zero.

Though 10^9 calls of rand()==0{exit} never terminated in my environment
(smallest rand() value was 3.25963e-09). I suppose one has to look into
the gawk sources to be sure.

For practical purpose I think it's sufficent; if you need random integer
numbers the existance of 0.0 isn't necessary (insignificant after you
scaled and converted the value), for random real numbers you may shift
the returned values to the negative and cut anything below 0.0.

Janis

>
> A search with Yahoo of "rand awk" shows a lot of webpages that aren't
> at all specific about the endpoints.
>
> Does anyone know why GAWK defined rand() differently? And are there
> any gotchas that need to be accomodated when writing AWK programs that
> might run under TAWK or might run under GAWK.
>
> Thanks,
>
> DKM
>
>
> To contact me directly, send EMAIL to (single letters all)
> DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]

Andrew Schorr

2007-02-16, 6:57 pm

On Feb 16, 2:19 am, Doug McClure <Dee_Kay_...@EarthLink.net> wrote:
> The Awk Programming Language book by Aho, Weinberger, and Kernighan
> states that the values returned from the rand() function are [0.0,
> 1.0). That is -- 0 is a possible value but 1.0 is not.
>
> I use Thompson AWK, TAWK, but usually refer to the GAWK Windows Help
> File when I have a question, and the GAWK Help file indicates that the
> range is (0.0, 1.0). This differs from the AWK Programming Language
> book in that 0.0 is not possible.


I'm not exactly sure what you're looking at, but the gawk.info docs
say:

`rand()'
This returns a random number. The values of `rand' are uniformly
distributed between zero and one. The value could be zero but is
never one.(1)

(1) The C version of `rand' 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, `gawk' uses the BSD `random' function, which is
considerably better than `rand', to produce random numbers.

And inside the source code in builtin.c:do_rand there is this comment:

/*
* Per historical practice and POSIX, return value N is
*
* 0 <= n < 1
*/

Regards,
Andy

Janis Papanagnou

2007-02-16, 6:57 pm

Janis Papanagnou wrote:
> Doug McClure wrote:
>
>
> Isn't that equivalent to what the gawk "User's Guide" (Ed.3/June 2004)
> says?
>
>
> Hmm.. - "The value could be zero but is never one." [from the above doc]
> seems not to exclude zero.
>
> Though 10^9 calls of rand()==0{exit} never terminated in my environment
> (smallest rand() value was 3.25963e-09). I suppose one has to look into
> the gawk sources to be sure.


An update about the empirical results...

After 2.6131e+09 calls of rand() a zero has been created with my gawk
(GNU Awk 3.1.1).

Janis
[color=darkred]
>
> For practical purpose I think it's sufficent; if you need random integer
> numbers the existance of 0.0 isn't necessary (insignificant after you
> scaled and converted the value), for random real numbers you may shift
> the returned values to the negative and cut anything below 0.0.
>
> Janis
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com