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: Nelson H. F. Beebe
Subject: Re: Subject: RE: Change seed for every iteration
Date: Fri, 3 Nov 2023 12:23:18 -0600

J Naman <jnaman2@gmail.com> reports a small code fragment for getting
a time-based seed for a random number generator.

Unfortunately, the proposal is unreliable.

The reason is that, while gettimeofday() returns a data structure with
two components, seconds and microseconds, it is wrong to conclude that
the latter has the granularity of its units.  It does not.  On many
systems, the time of day counter is based on a wall-clock timee that
ticks at the electrical frequency, typically 50Hz or 60Hz in most
countries.  Thus, the subsecond component cannot even measure a time
step of 0.01 seconds, or about 5 bits of entropy.

While some CPUs offer high-resolution timers that tick with the CPU
clock rate, thus, at nanosecond or better resolution, their interfaces
for C programmers vary widely across operating systems.  In addition,
on some systems, the returned times are fuzzed by the operating system
to prevent accurate timing, which could be use to trace steps in
cryptographic algorithms in an attempt to recover encryption keys.

What the proposal COULD do is blend in an iteration counter that steps
1, 2, 3, ...  That would give random number generator seeds that
differ enough to produce different streams.

The generator used in current gawk versions has a period that is
vastly larger than 32-bit or 64-bit periods can provide, so it is
close to impossible to get overlapping data streams when the starting
seeds differ.

See the footnotes in the gawk manual section 9.1.3 Numeric Functions
for details, or consult the source code comments in
gawk-5.3.0/support/random.c.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                                                          -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------



reply via email to

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