help-gawk
[Top][All Lists]
Advanced

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

Re: Change seed for every iteration


From: Hermann Peifer
Subject: Re: Change seed for every iteration
Date: Fri, 3 Nov 2023 19:12:16 +0100
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1

On 03/11/2023 15.42, Z wrote:
Hermann Peifer via Help-gawk <help-gawk@gnu.org> wrote:

Hi.

I am in search of an elegant way to change seed for every iteration that
my little AWK programme does. There are about 10-100s iterations per
second, I would guess. Below my current solution.

Is there a more abvious way to achieve the same (or a better) result,
e.g. without getline?

Hermann

PS: GAWK's strftime does not seem to understand '%N', this is why I am
using the system's date function.

          # Initialize random weights and biases
          cmd = "date +%s%N"
          while ( ( cmd | getline seed ) > 0 )
                  srand(seed)
          close(cmd)

You can load the time(3am) extension to get nanosecs:

        $ gawk -l 'time' 'BEGIN{printf("%.7f\n",gettimeofday())}'
        1699021867.0103779


For randomness I use the following in a silly "Magic 8 Ball" script:

        # generate random integer between 1 and N:
        srand(); R = int(N * rand()) + 1

Z

Thanks for the hint to the time extension. I should have known myself.

Regards, Hermann



reply via email to

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