The MASM Forum

Projects => MasmBasic & the RichMasm IDE => Topic started by: jj2007 on June 28, 2017, 05:30:58 AM

Title: Throwing the dice... fast
Post by: jj2007 on June 28, 2017, 05:30:58 AM
An example how to use the high speed Rand() (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1030) macro:

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  xor ecx, ecx
  Loops=3000
  Rand()
  NanoTimer()
  Dim dice(7) As DWORD
  lea edi, dice(0)
  .Repeat
      inc Rand(6)                    ; Rand(6) returns integers from 0...5, so we add 1 to get 1...6
      inc dword ptr [edi+4*eax]      ; inc dice(eax) to count the matches
      inc ecx
  .Until ecx>Loops*1000000
  PrintCpu 0
  Print Str$("%i ms", NanoTimer(ms)), Str$(" for %i Million loops\n", Loops)
  For_ ecx=0 To 7
      PrintLine Str$(ecx), Tb$, Str$(dice(ecx))
  Next
EndOfCode


Output:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
10718 ms for 3000 Million loops
0       0
1       500011628
2       499995413
3       500003267
4       499999652
5       499984295
6       500005746
7       0
Title: Re: Throwing the dice... fast
Post by: Siekmanski on June 28, 2017, 07:24:49 AM
Code: [Select]
Intel(R) Core(TM) i7-4930K CPU @ 3.40GHz
9430 ms for 3000 Million loops
0       0
1       500010164
2       499990657
3       499990338
4       499988862
5       500010734
6       500009246
7       0
Title: Re: Throwing the dice... fast
Post by: LiaoMi on June 28, 2017, 05:03:13 PM
Code: [Select]
Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz
8919 ms for 3000 Million loops
0       0
1       500006877
2       500000600
3       499995703
4       499998108
5       499994077
6       500004636
7       0

Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz
8900 ms for 3000 Million loops
0       0
1       500008899
2       499997171
3       500014229
4       500000482
5       499973446
6       500005774
7       0
Title: Re: Throwing the dice... fast
Post by: jj2007 on June 28, 2017, 06:32:33 PM
Thanks, folks :biggrin:

It was not really meant as a Lab exercise, but if you insist: Here is an improved version.

For those interested in fast and good random generators, attached an overview of available random generators extracted from the CAcert Research Lab (http://www.cacert.at/cgi-bin/rngresults) site. They are sorted by speed, but check the top twenty for quality, too. MasmBasic Rand() (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1030) has one minor weak point called "6x8 matrix rank", apart from that it's in the top range for randomness. Alex Bagayev and myself developed it about 7 years ago, the exercise starts roughly here (http://www.masmforum.com/board/index.php?topic=11679.msg121920#msg121920) :icon_cool: