help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subject: RE: Change seed for every iteration


From: Andrew J. Schorr
Subject: Re: Subject: RE: Change seed for every iteration
Date: Fri, 3 Nov 2023 14:07:08 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

FYI, on Linux, the time extension's gettimeofday function is currently using
the gettimeofday system call, which returns the time with microsecond
precision. So that's just 6 digits after the decimal. Perhaps we should patch
the code to use clock_gettime instead...

Regards,
Andy

On Fri, Nov 03, 2023 at 01:37:28PM -0400, J Naman wrote:
> When I do seed setting, I aim for 30 bits (integer) from time
> Using awk::gettimeofday()
> The first 10 decimal digits are seconds, so ignore
> The next   8 digits are subsecond time value == 10 nanosec
> The rest are effectively noise IMHO no value for seeding
> 
> @load "time"
> # get 9 digits = 30 bit integer from time to use as srand() seed
> # using largest number of relevant bits available
> # ns=nanosecond= 10^-9; 1/1,000,000,000=> fmt=".9f"
> 
> function awk::rand_seed_from_time( x)
> { x=awk::gettimeofday()
> # example 1677689070.911245822906494140625000
> # 1677689070 = 10 digits == secs since 1900
> # .91124582 = 8 digits subsecond time value
> # get 8 decimal digits of accuracy == 10 nanosecs
>  x=int(x*(10^8))    # truncate the noise below 10 nanosec
>  return x%(10^9)  # return 9 (not 8) digits = 30 bits
> }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]