luser droog
2022-12-01 02:09:10 UTC
I was trying to play with making a random number stream
and the numbers that work in C result in a very short period
when PostScript's auto-promoting mul is used.
Is there a way to do an integer multiply that truncates the
result to an integer ... in PS? Or some other way to generate
random numbers with the operators available in PostScript?
/rand_next 1 def
/fmod { 2 copy div truncate mul sub } def
/shuffle {
1103515245.0 mul 12345.0 add 16#100000000 fmod
} def
/zrand {
/rand_next rand_next shuffle def
rand_next cvi 16 bitshift
/rand_next rand_next shuffle def
rand_next cvi 16#FFFF and or
} def
10 { zrand = } repeat
rand_next =
/ =
/randstep { % rand_next -> [ rand_value { rand_next' randstep } ]
shuffle
dup cvi 16 bitshift exch
shuffle
dup cvi 16#FFFF and 3 2 roll or exch
/randstep cvx 2 array astore cvx 2 array astore
} def
1 randstep
10 {
aload pop exch =
exec
} repeat
==
$ gsnd rand.ps
GPL Ghostscript 9.55.0 (2021-09-27)
Copyright (C) 2021 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
72320781647872
809041920
12345
232959026135040
809041920
12345
232959026135040
809041920
12345
232959026135040
0.0
72320781647872
809041920
12345
232959026135040
809041920
12345
232959026135040
809041920
12345
232959026135040
[809041920 {3.55467264e+09 randstep}]
GS> quit
and the numbers that work in C result in a very short period
when PostScript's auto-promoting mul is used.
Is there a way to do an integer multiply that truncates the
result to an integer ... in PS? Or some other way to generate
random numbers with the operators available in PostScript?
/rand_next 1 def
/fmod { 2 copy div truncate mul sub } def
/shuffle {
1103515245.0 mul 12345.0 add 16#100000000 fmod
} def
/zrand {
/rand_next rand_next shuffle def
rand_next cvi 16 bitshift
/rand_next rand_next shuffle def
rand_next cvi 16#FFFF and or
} def
10 { zrand = } repeat
rand_next =
/ =
/randstep { % rand_next -> [ rand_value { rand_next' randstep } ]
shuffle
dup cvi 16 bitshift exch
shuffle
dup cvi 16#FFFF and 3 2 roll or exch
/randstep cvx 2 array astore cvx 2 array astore
} def
1 randstep
10 {
aload pop exch =
exec
} repeat
==
$ gsnd rand.ps
GPL Ghostscript 9.55.0 (2021-09-27)
Copyright (C) 2021 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
72320781647872
809041920
12345
232959026135040
809041920
12345
232959026135040
809041920
12345
232959026135040
0.0
72320781647872
809041920
12345
232959026135040
809041920
12345
232959026135040
809041920
12345
232959026135040
[809041920 {3.55467264e+09 randstep}]
GS> quit