Author Topic: RANDOM NUMBERS  (Read 13199 times)

frktons

  • Member
  • ***
  • Posts: 491
RANDOM NUMBERS
« 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

Vortex

  • Member
  • *****
  • Posts: 2109
Re: RANDOM NUMBERS
« Reply #1 on: December 04, 2012, 05:23:21 AM »
Hi Frank,

You can test this code :

Code: [Select]

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

jj2007

  • Member
  • *****
  • Posts: 10115
  • Assembler is fun ;-)
    • MasmBasic
Re: RANDOM NUMBERS
« Reply #2 on: December 04, 2012, 05:45:43 AM »
Based on Alex' algo - top quality tested with FourmiLab's ENT ;-)

include \masm32\MasmBasic\MasmBasic.inc   ; download

.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

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: RANDOM NUMBERS
« Reply #3 on: December 04, 2012, 05:45:58 AM »
using the Cryptography API:

Code: [Select]
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
MREAL macros - when you need floating point arithmetic while assembling!

frktons

  • Member
  • ***
  • Posts: 491
Re: RANDOM NUMBERS
« Reply #4 on: December 04, 2012, 05:47:47 AM »
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
Code: [Select]
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:

frktons

  • Member
  • ***
  • Posts: 491
Re: RANDOM NUMBERS
« Reply #5 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:

Quote
include \masm32\MasmBasic\MasmBasic.inc   ; download

.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?

jj2007

  • Member
  • *****
  • Posts: 10115
  • Assembler is fun ;-)
    • MasmBasic
Re: RANDOM NUMBERS
« Reply #6 on: December 04, 2012, 06:36:32 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.

frktons

  • Member
  • ***
  • Posts: 491
Re: RANDOM NUMBERS
« Reply #7 on: December 04, 2012, 06:56:38 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:

jj2007

  • Member
  • *****
  • Posts: 10115
  • Assembler is fun ;-)
    • MasmBasic
Re: RANDOM NUMBERS
« Reply #8 on: December 04, 2012, 07:42:24 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.

frktons

  • Member
  • ***
  • Posts: 491
Re: RANDOM NUMBERS
« Reply #9 on: December 04, 2012, 10:12:42 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

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: RANDOM NUMBERS
« Reply #10 on: December 06, 2012, 10:32:12 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.

frktons

  • Member
  • ***
  • Posts: 491
Re: RANDOM NUMBERS
« Reply #11 on: December 06, 2012, 11:12:07 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.

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: RANDOM NUMBERS
« Reply #12 on: December 06, 2012, 11:18:09 AM »
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.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7059
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: RANDOM NUMBERS
« Reply #13 on: December 06, 2012, 03:20:46 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: