I'd like to fill a 4096 bytes array with random numbers
in the range 0-255. I actually don't have any proc to generate
these numbers, but I'm quite confident someone do in the nearby.
Could you post some routine/macro/library call please?
Thanks
Frank
Hi Frank,
You can test this code :
include RandomNumb.inc
.data
format1 db 'Random number = %d',13,10,0
.code
start:
call main
invoke ExitProcess,0
main PROC uses ebx
LOCAL _st:SYSTEMTIME
invoke GetSystemTime,ADDR _st
movzx eax,SYSTEMTIME.wMilliseconds[_st]
invoke crt_srand,eax
mov ebx,10
@@:
invoke crt_rand
and eax,0000000FFh
invoke crt_printf,ADDR format1,eax
dec ebx
jnz @b
ret
main ENDP
END start
Based on Alex' algo - top quality tested with FourmiLab's ENT (http://www.fourmilab.ch/random/) ;-)
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
.data?
MyArray db 4096 dup(?)
Init
mov edi, offset MyArray
mov ecx, sizeof MyArray
.Repeat
void Rand(256)
stosb
dec ecx
.Until Zero?
Inkey "ok"
Exit
end start
using the Cryptography API:
include \masm32\include\Advapi32.inc
includelib \masm32\lib\Advapi32.lib
...
RandomBytes proc dwLength:DWORD,pBuffer:PVOID
LOCAL hProvider:HANDLE
fn CryptAcquireContext,&hProvider,0,0,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT or CRYPT_SILENT
fn CryptGenRandom,hProvider,dwLength,pBuffer
fn CryptReleaseContext,hProvider, 0
ret
RandomBytes endp
Thanks Vortex. It's probably from the oldies of yours,
and I had to add a couple of instructions to see the
results, and of course it works. :t
Random number = 171
Random number = 123
Random number = 2
Random number = 51
Random number = 65
Random number = 102
Random number = 123
Random number = 100
Random number = 33
Random number = 176
Press any key to continue ...
And thanks qWord for the alternative, very instructional,
and Jochen that has almost anything for any occasion in MasmBasic :lol:
If I'd like to fill one Array with the random numbers generated,
and another one with the same numbers in reverse order, using
Jochen's routine:
Quote
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
.data?
MyArray db 4096 dup(?)
MyRevArray db 4096 dup(?)
Init
mov edi, offset MyArray
mov ecx, sizeof MyArray
lea edx, [MyRevArray +ecx -1]
.Repeat
void Rand(256)
stosb
mov byte ptr[edx], al
dec edx
dec ecx
.Until Zero?
Inkey "ok"
Exit
end start
Should it work?
Quote from: frktons on December 04, 2012, 06:26:28 AM
If I'd like to fill one Array with the random numbers generated,
and another one with the same numbers in reverse order, using
Jochen's routine:
Should it work?
Yes.
Quote from: jj2007 on December 04, 2012, 06:36:32 AM
Quote from: frktons on December 04, 2012, 06:26:28 AM
If I'd like to fill one Array with the random numbers generated,
and another one with the same numbers in reverse order, using
Jochen's routine:
Should it work?
Yes.
Great, today I earned the noble title of
MasmBasic Student :eusa_dance:
Quote from: frktons on December 04, 2012, 06:56:38 AM
Great, today I earned the noble title of MasmBasic Student :eusa_dance:
:greensml:
But attention: Avoid using edx - line 3214 in MasmBasic.inc is a very special case. Normally edx is the least useful register and gets merciless trashed. Only ecx (plus the usual esi edi ebx ebp) is safe, and even that one gets trashed if you invoke a non-MasmBasic function.
Quote from: jj2007 on December 04, 2012, 07:42:24 AM
:greensml:
But attention: Avoid using edx - line 3214 in MasmBasic.inc is a very special case. Normally edx is the least useful register and gets merciless trashed. Only ecx (plus the usual esi edi ebx ebp) is safe, and even that one gets trashed if you invoke a non-MasmBasic function.
OK Jochen
Quote from: frktons on December 04, 2012, 05:16:51 AM
I'd like to fill a 4096 bytes array with random numbers
in the range 0-255. I actually don't have any proc to generate
these numbers, but I'm quite confident someone do in the nearby.
Could you post some routine/macro/library call please?
Thanks
Frank
Frank,
Test several different RNGs, save the results to a file, then run ENT (http://www.fourmilab.ch/random/) to check the randomness.
One method that I found that gave very good results was to pick a prime number in the period (256 in your case), then start at 0 (or any random number in the period) and add the prime number mod the period (256 in your case) and save the results, for your case, just add the prime to al and save al - no masking needed. This will give a full set of non matching random numbers for the period.
Dave.
Quote from: KeepingRealBusy on December 06, 2012, 10:32:12 AM
Frank,
Test several different RNGs, save the results to a file, then run ENT (http://www.fourmilab.ch/random/) to check the randomness.
One method that I found that gave very good results was to pick a prime number in the period (256 in your case), then start at 0 (or any random number in the period) and add the prime number mod the period (256 in your case) and save the results, for your case, just add the prime to al and save al - no masking needed. This will give a full set of non matching random numbers for the period.
Dave.
Thanks Dave. Good suggestion to think upon.
Frank,
The random number to pick should be about 3/4 of the period, at least that is what I have been using.
You can use different prime numbers to get different number ordering, and can start at any value and generate the next 255. Lots of variation available.
Dave.
Frank,
Don't be afraid to use the Park Miller random number algorithm (nrandom algo) in the masm32 library, its been doing the job with good results tested with ENT for a long time.