Author Topic: Random number algorithm.  (Read 577 times)

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Random number algorithm.
« Reply #15 on: July 31, 2017, 09:37:36 PM »
For a 1 instruction addition, it went from a dud to a kick ass random generator.

DualRand.inc, line 48:
Code: [Select]
  bswap eax ; we need the high order bits
And the timings are already quite ok:
Code: [Select]
811 ticks for irand
687 ticks for Rand64
This code was assembled with ml64 in 64-bit format

You see, it wasn't that difficult :P


jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Random number algorithm.
« Reply #16 on: August 05, 2017, 08:55:24 PM »
It can be improved but not by much. Source and test piece attached. Run the executable, the batch file is to run ENT.

Hutch,

I get this build error using www.masm32.com/download/x64make.zip from the Latest build environment for ML64:
Code: [Select]
Microsoft (R) Macro Assembler (x64) Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: xorshift.asm
xorshift.asm(56) : error A2008:syntax error : .exit

Does it require a new version of the macros that you have not yet posted? Or did you post it somewhere else?

(did it build for anybody else? if yes, which settings/macro files?)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random number algorithm.
« Reply #17 on: August 05, 2017, 11:04:07 PM »
> Yes and don't know. I have already posted the 64 bit random algo, the rest may come as I upgrade the libraries and macros.

This is the most recent versions I have posted,

http://masm32.com/board/index.php?topic=6365.0

The 64 bit version is a work in progress, the random app has code that has yet to be published so if you really need it, you can use the option that you offered me, disassemble it.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Random number algorithm.
« Reply #18 on: August 05, 2017, 11:24:05 PM »
you can use the option that you offered me, disassemble it.

Your sense of humour is back, great :t

Vortex

  • Member
  • *****
  • Posts: 1704
Re: Random number algorithm.
« Reply #19 on: August 07, 2017, 06:05:30 AM »
qWord recommends using Window's Cryptography API. Code translated to Masm64 :

http://masm32.com/board/index.php?topic=2735.msg28966#msg28966

Code: [Select]
RandomBytes PROC dwLength:QWORD,pBuffer:QWORD

LOCAL dummy:QWORD
LOCAL hProvider:QWORD

    sub     rsp,5*8+8
    mov     dwLength,rcx
    mov     pBuffer,rdx
   
    invoke  CryptAcquireContext,ADDR hProvider,0,0,\
            PROV_RSA_FULL,\
            CRYPT_VERIFYCONTEXT or CRYPT_SILENT
           
    invoke  CryptGenRandom,hProvider,dwLength,pBuffer
    invoke  CryptReleaseContext,hProvider,0
    ret

RandomBytes ENDP

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4813
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random number algorithm.
« Reply #20 on: August 07, 2017, 09:58:36 AM »
Thanks Erol.  :t
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Random number algorithm.
« Reply #21 on: August 07, 2017, 12:21:03 PM »
Nice example, Erol :t

CryptGenRandom is very slow, though:
Code: [Select]
This code was assembled with ml64 in 64-bit format
result  -8032366409749840727
result  87096612809990884
result  -3122102231650220507
result  6210554543873536060
result  7239442655864793548
result  3289759298143981941
46067 ticks for CryptGenRandom

result  9056870149978556565
result  -1890820636788552871
result  2387620311255054330
result  37962361222013295
result  835162364812257345
result  4123339310970107070
0 ticks for Rand64()

Interesting: The 64-bit version is exactly twice as fast - as if it was called twice internally ::)