flat assembler
Message board for the users of flat assembler.

 Index > Tutorials and Examples > pmrand
Author
Ali.Z

Joined: 08 Jan 2018
Posts: 589
Ali.Z 20 Feb 2023, 14:44
Code:
```A = 41A7h ; for better statistics use BC8Fh
M = 7FFFFFFFh
Q = M / A
R = M mod A

pmrand:
mov eax,[s]
mov ecx,Q
cdq ;fixed -2 issue
idiv ecx
imul eax,R
imul edx,A
xchg eax,edx
sub eax,edx
jns @f
@@:
mov [s],eax
ret ;eax ret value

;...

s dd 1 ;must be non-zero and not equ to M
```

_________________
Asm For Wise Humans

Last edited by Ali.Z on 21 Feb 2023, 05:45; edited 3 times in total
20 Feb 2023, 14:44
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 19256
revolution 21 Feb 2023, 03:58
A input value of -2 in [s] will generate zero. And the same for the (-2) shr 1 (= 0x7fffffff).

What do you mean by "better statistics"? Which statistics are you referring to?
21 Feb 2023, 03:58
Ali.Z

Joined: 08 Jan 2018
Posts: 589
Ali.Z 21 Feb 2023, 05:10
input value of 0x7FFFFFFF should not generate 0, this is silly mistake by me; I will fix it. (but there is no difference, even if I fix it, it would repeat 0x7FFFFFFF forever)
as for -2 generating 0, I really don't know; I will figure it out later.

better number distribution statistics (weighting).
note: it returns a value in open interval (0,M)

_________________
Asm For Wise Humans

Last edited by Ali.Z on 21 Feb 2023, 05:23; edited 2 times in total
21 Feb 2023, 05:10
bitRAKE

Joined: 21 Jul 2003
Posts: 3627
Location: vpcmipstrm
bitRAKE 21 Feb 2023, 05:13
So, [s] range would be [1,0x7FFFFFFE], making 0x7FFFFFFF an invalid input?
21 Feb 2023, 05:13
Ali.Z

Joined: 08 Jan 2018
Posts: 589
Ali.Z 21 Feb 2023, 05:18
yes.

Ali.Z wrote:
I will fix it. (but there is no difference, even if I fix it, it would repeat 0x7FFFFFFF forever)

actually getting zero is good (if initial state is equ M), but if anyone don't want to get zero, then replace jns with jg. (in real world, it would never generate 0, the only way to get zero is by having initial state equ to M)

and fixed -2 issue.

_________________
Asm For Wise Humans
21 Feb 2023, 05:18
bitRAKE

Joined: 21 Jul 2003
Posts: 3627
Location: vpcmipstrm
bitRAKE 21 Feb 2023, 15:37
The original Park-Miller article from 1988 can be found here:
http://www.cems.uwe.ac.uk/~irjohnso/coursenotes/ufeen8-15-m/p1192-parkmiller.pdf

... it says the A value was originally suggested in 1969.
21 Feb 2023, 15:37
Ali.Z

Joined: 08 Jan 2018
Posts: 589
Ali.Z 21 Feb 2023, 16:46

_________________
Asm For Wise Humans
21 Feb 2023, 16:46
bitRAKE

Joined: 21 Jul 2003
Posts: 3627
Location: vpcmipstrm
bitRAKE 21 Feb 2023, 17:45
Nice, that links to the 1969 paper - which outlines the original testing method.
(Lehmer actually suggests 14^29 for the A, in 1954.)

_________________