[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Change seed for every iteration
From: |
Z |
Subject: |
Re: Change seed for every iteration |
Date: |
Fri, 03 Nov 2023 14:42:33 +0000 |
User-agent: |
mail v14.9.24 |
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