I was wondering what is a good / or best way to generate random numbers?
Need a break from my custom menu code.
I know IWB has the Rand / Rnd commands, but how random are they or do they follow some formula (guess they'd have to?).
Graham wrote this for random numbers:
http://www.ionicwind.com/forums/index.php?topic=2275.msg19793#msg19793
But it's a dll written for Cbasic, not IWB.
Any ideas / thoughts anyone on generating random numbers say from 1 to 100?
Thanks,
Andy.
:)
Andy,
By all that I have read, it is really hard to produce truly unrepeating random numbers. Best suggestion I saw was to use the GetTickCount() API function and use the result as the seed
Brian
hello Andy
have looked at Graham's dll source ?
it's very short and should be easy to adapt.
RAND / RND commands are probably good enough for most applications.
If you are really bored, you could write your own procedure. Below is MWC random function:
DECLARE "kernel32.dll", GetTickCount(),int
int i
for i=1 to 100
print rand1(1000)
next i
waitcon
end
$option "/p 1"
sub rand1(uint num), uint
uint rn
_asm
extern GetTickCount
segment .data
random_var: dd 0,0
segment .text
cmp dword [random_var], 0
jnz skip
call GetTickCount
mov dword [random_var], eax
call GetTickCount
mov dword [random_var+4], eax
skip:
mov ebx, dword [num]
cmp ebx, 0
jz random_exit
cmp ebx, 0xFFFFFFFF
jz random_exit
mov eax, 0x4019CF16
mul dword [random_var]
add eax, dword [random_var+4]
mov dword [random_var], eax
adc edx, 0
mov dword [random_var+4], edx
mov edx, 0
inc ebx
div ebx
xchg edx, eax
random_exit:
mov dword [rn], eax
_endasm
return rn
endsub
$option "/p 0"
Thanks everyone for your input.
I'm looking at Graham's code as we speak, can anyone explain the difference of normal distribution and random (or am I missing something blindingly obvious - probably!). :o
Great little code Graham.
Jalih's code is also very interesting, had to comment out the last line to compile it - nice one!
Andy.
:)
Quote from: Andy on March 20, 2018, 07:40:13 AM
Jalih's code is also very interesting, had to comment out the last line to compile it - nice one!
It might be a good idea to write a separate procedure for setting up the random seed
DECLARE "kernel32.dll", GetTickCount(),int
randomize1()
int i
for i=1 to 100
print rand1(100)
next i
waitcon
end
_asm
segment .data
random_var: dd 5,3
segment .text
_endasm
sub randomize1()
_asm
extern GetTickCount
call GetTickCount
mov dword [random_var], eax
call GetTickCount
mov dword [random_var+4], eax
_endasm
endsub
$option "/p 1"
sub rand1(uint num), uint
uint rn
_asm
mov ebx, dword [num]
cmp ebx, 0
jz random_exit
cmp ebx, 0xFFFFFFFF
jz random_exit
mov eax, 0x4019CF16
mul dword [random_var]
add eax, dword [random_var+4]
mov dword [random_var], eax
adc edx, 0
mov dword [random_var+4], edx
mov edx, 0
inc ebx
div ebx
xchg edx, eax
random_exit:
mov dword [rn], eax
_endasm
return rn
endsub
$option "/p 0"