Author Topic: Throwing the dice... fast  (Read 244 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7661
  • Assembler is fun ;-)
    • MasmBasic
Throwing the dice... fast
« on: June 28, 2017, 05:30:58 AM »
An example how to use the high speed Rand() macro:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  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

Siekmanski

  • Member
  • *****
  • Posts: 1118
Re: Throwing the dice... fast
« Reply #1 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

LiaoMi

  • Member
  • **
  • Posts: 155
Re: Throwing the dice... fast
« Reply #2 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

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7661
  • Assembler is fun ;-)
    • MasmBasic
Re: Throwing the dice... fast
« Reply #3 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 site. They are sorted by speed, but check the top twenty for quality, too. MasmBasic Rand() 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 :icon_cool: