News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Monte Carlo Simulation with RDRAND (32 bit)

Started by Gunther, October 07, 2013, 09:32:58 PM

Previous topic - Next topic

Antariy

Oh, was posting simultaneously :biggrin:
Thank you, John! :t

jj2007

One more :biggrin:
Generating 200 Million random numbers with C.
That'll take a little while ...

Area           = 0.250098180000000
Absolute Error = 0.000098180000000
Elapsed Time   = 14.77 Seconds

Generating 200 Million random numbers with ASM Axrand.
That'll take a little while ...

Area           = 0.249965115000000
Absolute Error = 0.000034885000000
Elapsed Time   = 7.27 Seconds

Antariy

Quote from: sinsi on October 11, 2013, 08:43:59 PM
What are the numbers for area and error e.g. is 25.002 better than 24.999?

If I'm not mistaken, AFAIK the "ideal PRNG" result should be equal to the 0.25, so, the closer to this is the result, the better ("more" random distribution of the numbers) the PRNG output is. The difference though between generators in this test is that RDRAND and Axrand return numbers in the DWORD range, but the C rand returns the numbers in the range of 0...32767.


Gunther

Hi Alex,

Quote from: Antariy on October 11, 2013, 01:15:24 PM
Thank you, Gunther, for permission to add the code into your test :biggrin:

You're welcome, Alex. Thank you for testing the C source with MS VC.

Here is the output:

Generating 200 Million random numbers with RDRAND.
That'll take a little while ...

Area           = 0.250033590000000
Absolute Error = 0.000033590000000
Elapsed Time   = 16.08 Seconds

Generating 200 Million random numbers with C.
That'll take a little while ...

Area           = 0.250033955000000
Absolute Error = 0.000033955000000
Elapsed Time   = 5.02 Seconds

Generating 200 Million random numbers with ASM Axrand.
That'll take a little while ...

Area           = 0.249965115000000
Absolute Error = 0.000034885000000
Elapsed Time   = 2.89 Seconds

This is empty reference loop to take the calculation code time in account
That'll take a little while ...

Area           = 0.000000000000000
Absolute Error = 0.250000000000000
Elapsed Time   = 1.50 Seconds


Gunther
You have to know the facts before you can distort them.

Antariy

Quote from: Gunther on October 11, 2013, 10:21:38 PM
You're welcome, Alex. Thank you for testing the C source with MS VC.

Thank you, Gunther! You're welcome, too! :biggrin:

jj2007

include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  Dll "NtDll"                   ; don't believe MSDN, it's NtDll, really...
  Declare RtlRandomEx, 1        ; _Inout_  PULONG Seed
  push "Ciao"
  push "Alex"
  m2m ebx, 20
  .Repeat
        PrintLine Str$(edx::RtlRandomEx(esp))
        dec ebx
  .Until Sign?
  pop eax
  pop edx
  Inkey " ok"
  Exit
end start


Output:
4791245291978797284
1200059578463366776
...
7431188666248921181
6381367696984855216

;)

Gunther

Jochen,

interesting code. But why pushing "Ciao" and "Alex"?

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: Gunther on January 26, 2014, 10:17:04 PM
interesting code. But why pushing "Ciao" and "Alex"?
If you prefer another qword seed, just
push "Gunt"
push "her"
;-)

Gunther

Jochen,

Quote from: jj2007 on January 26, 2014, 10:41:49 PM
If you prefer another qword seed, just
push "Gunt"
push "her"
;-)

that makes more sense.  :lol: :lol: :lol:

Gunther
You have to know the facts before you can distort them.

jj2007

After reading on MSDN...
QuoteThe RtlRandomEx function is an improved version of the RtlRandom function. Compared with the RtlRandom function, RtlRandomEx is twice as fast and produces better random numbers
... I could not resist the temptation to make some tests. Here are the results :dazzled:

1521 ms with writing the file, RtlRandomEx
121 ms without writing

1336 ms with writing the file, MasmBasic Rand()
2 ms without writing


############ ENT results RtlRandomEx:
Entropy = 7.953506 bits per byte.

Optimum compression would reduce the size
of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 63775.98, and randomly
would exceed this value 0.01 percent of the times.

Arithmetic mean value of data bytes is 111.5300 (127.5 = random).
Monte Carlo value for Pi is 3.491725967 (error 11.15 percent).
Serial correlation coefficient is -0.047085 (totally uncorrelated = 0.0).

############ ENT results MasmBasic Rand():
Entropy = 7.999827 bits per byte.

Optimum compression would reduce the size
of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 240.28, and randomly
would exceed this value 50.00 percent of the times.

Arithmetic mean value of data bytes is 127.4222 (127.5 = random).
Monte Carlo value for Pi is 3.141804567 (error 0.01 percent).
Serial correlation coefficient is -0.002690 (totally uncorrelated = 0.0).

Gunther

Jochen,

here are the test results:

351 ms with writing the file, RtlRandomEx
45 ms without writing

283 ms with writing the file, MasmBasic Rand()
1 ms without writing

############ ENT results RtlRandomEx:
Entropy = 7.953470 bits per byte.

Optimum compression would reduce the size
of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 63817.08, and randomly
would exceed this value 0.01 percent of the times.

Arithmetic mean value of data bytes is 111.4225 (127.5 = random).
Monte Carlo value for Pi is 3.489901960 (error 11.09 percent).
Serial correlation coefficient is -0.048078 (totally uncorrelated = 0.0).

############ ENT results MbRand:
Entropy = 7.999827 bits per byte.

Optimum compression would reduce the size
of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 240.28, and randomly
would exceed this value 50.00 percent of the times.

Arithmetic mean value of data bytes is 127.4222 (127.5 = random).
Monte Carlo value for Pi is 3.141804567 (error 0.01 percent).
Serial correlation coefficient is -0.002690 (totally uncorrelated = 0.0).


Gunther
You have to know the facts before you can distort them.

MichaelW

I spent several hours testing RtlRandomEx under Windows XP, and while most of the ENT results were reasonable, the Chi-square distribution was always far from reasonable. Looking at the RtlRandomEx code in a debugger it's not anything trivial, and it uses the bottom 7 bits of the seed to load a DWORD from an apparently 128-element table, so I suspect that under Windows XP the function is not operating as intended.
Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Michael,

Results are the same on Windows 7. The mean is also too far off to be credible.
What I found interesting is that it modifies the seed's location, and you can use that value to get a 64-bit result with practically identical scores.

Gunther

I've tested it under Windows 7 (64 bit). I can test it under Windows 7, 32 bit version and WindowsXP.

Gunther
You have to know the facts before you can distort them.