|
On Wed, 25 Apr 2001, DeLong, Eric wrote:
> True, I added the time seed shortly after posting the example, and I too
> realized that Hans' example was repeating sequences. It also occurs to me
> that seeding by TIME (6.0) could be a bad idea is the program runs at the
> same time every night (scheduled job). It's not hard to imagine the same
> seed value being used repeatedly...... perhaps something more random like
> milliseconds....
>
> > Caveat: every time you run this, you will get the same sequence of
> > `random' numbers. To get more truly pseudo-random numbers, seed the
> > generator with (say) the result of the TIME opcode.
Call me a purist, but... time is about the LEAST random thing I can
think of -- it follows a rather well-established pattern. :)
It may be true that when I'm typing the command to run a program on my
AS/400, I can't predict at what millisecond the program will run -- but
if I were a computer program, or a job scheduler, etc, my program would
run at a rather predictable time.
Even if the time a job starts varies a little now, as computers get faster
it'll become more and more consistent!
Therefore, I like to stick elements from the JOB NUMBER and DATE into my
seed as well as the milliseconds. (Actually, I use microseconds) This
makes me feel better. :)
Here's an example:
H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')
D rand PR 10I 0 extproc('rand')
D srand PR extproc('srand')
D seed 10U 0 value
D gettimeofday PR ExtProc('gettimeofday')
D timeval 8A
D timezone 8A
D timeval DS
D tv_secs 10I 0
D tv_usecs 10I 0
D sds
D JobNo 264 269
D Junk S 8A
D Epoch S Z INZ(z'1970-01-01-00.00.00.000000')
D UTC S Z
D Date S D
D Micro S 6S 0
D DateStr S 10A
D MicroStr S 6A
D Seed S 10U 0
D SeedStr S 10A
D num s 10I 0
D i s 10I 0
** get current time
c callp gettimeofday(timeval: junk)
** convert to a timestamp
c Epoch adddur tv_secs:*S UTC
c adddur tv_usecs:*MS UTC
** get date & microseconds in a char string:
c move UTC Date
c *ISO move Date DateStr
c extrct UTC:*MS Micro
c move Micro MicroStr
** make a seed
C eval SeedStr = %subst(DateStr: 9: 1)
c + %subst(MicroStr: 6: 1)
c + %subst(JobNo: 6: 1)
c + %subst(DateStr: 10: 1)
c + %subst(MicroStr: 2: 1)
c + %subst(JobNo: 5: 1)
c + %subst(MicroStr: 1: 1)
c + %subst(MicroStr: 3: 3)
c move SeedStr Seed
c callp srand(Seed)
c for i = 1 to 10
c eval num = rand
c num dsply
c endfor
c eval *inlr = *on
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.