Author Topic: Monte Carlo Simulation with RDRAND (32 bit)  (Read 28470 times)

Antariy

  • Member
  • ****
  • Posts: 551
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #15 on: October 11, 2013, 08:49:18 PM »
Oh, was posting simultaneously :biggrin:
Thank you, John! :t

jj2007

  • Member
  • *****
  • Posts: 11450
  • Assembler is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #16 on: October 11, 2013, 08:53:25 PM »
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

  • Member
  • ****
  • Posts: 551
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #17 on: October 11, 2013, 08:54:42 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.

Antariy

  • Member
  • ****
  • Posts: 551
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #18 on: October 11, 2013, 08:56:19 PM »

Gunther

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #19 on: October 11, 2013, 10:21:38 PM »
Hi Alex,

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:
Code: [Select]
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
Get your facts first, and then you can distort them.

Antariy

  • Member
  • ****
  • Posts: 551
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #20 on: October 11, 2013, 11:22:16 PM »
You're welcome, Alex. Thank you for testing the C source with MS VC.

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

jj2007

  • Member
  • *****
  • Posts: 11450
  • Assembler is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #21 on: January 26, 2014, 06:46:25 PM »
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

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #22 on: January 26, 2014, 10:17:04 PM »
Jochen,

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

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 11450
  • Assembler is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #23 on: January 26, 2014, 10:41:49 PM »
interesting code. But why pushing "Ciao" and "Alex"?
If you prefer another qword seed, just
push "Gunt"
push "her"
;-)

Gunther

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #24 on: January 26, 2014, 10:55:44 PM »
Jochen,

If you prefer another qword seed, just
push "Gunt"
push "her"
;-)

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

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 11450
  • Assembler is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #25 on: January 27, 2014, 03:30:31 AM »
After reading on MSDN...
Quote
The 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

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #26 on: January 27, 2014, 03:43:13 AM »
Jochen,

here are the test results:
Code: [Select]
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
Get your facts first, and then you can distort them.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #27 on: January 29, 2014, 04:53:36 AM »
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

  • Member
  • *****
  • Posts: 11450
  • Assembler is fun ;-)
    • MasmBasic
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #28 on: January 29, 2014, 05:24:51 AM »
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

  • Member
  • *****
  • Posts: 3722
  • Forgive your enemies, but never forget their names
Re: Monte Carlo Simulation with RDRAND (32 bit)
« Reply #29 on: January 29, 2014, 09:40:16 PM »
I've tested it under Windows 7 (64 bit). I can test it under Windows 7, 32 bit version and WindowsXP.

Gunther
Get your facts first, and then you can distort them.