Author Topic: Generating random numbers  (Read 4958 times)

Diamond Star

  • Regular Member
  • *
  • Posts: 12
Generating random numbers
« on: December 15, 2013, 10:11:04 AM »
Hello
I searched about this subject and find some topics include long and complicated codes
and its not reliable ,So can someone help in putting reliable code do this
and the code should generate new number every time it runs ,not like some codes I saw that generate the same number every time the program runs

jj2007

  • Member
  • *****
  • Posts: 10319
  • Assembler is fun ;-)
    • MasmBasic
Re: Generating random numbers
« Reply #1 on: December 15, 2013, 10:22:13 AM »
include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  Rand()                ; initialise
  push 100
  .Repeat
        Print Str$(Rand(1000)), " "
        dec stack
  .Until Sign?
  Inkey "ok?"
  Exit
end start

Alex is working on a new version, but even this one is simple, extremely reliable (in terms of ENT performance), and it generates a new series every time you launch the prog. And it's one of the fastest PRNGs available.

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Generating random numbers
« Reply #2 on: December 15, 2013, 10:53:44 AM »
A simple solution is to use Window's Cryptography API.
Code: [Select]
include \masm32\include\Advapi32.inc
includelib \masm32\lib\Advapi32.lib
...
RandomBytes proc dwLength:DWORD,pBuffer:PVOID
LOCAL hProvider:HANDLE

    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
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Generating random numbers
« Reply #3 on: December 15, 2013, 11:03:09 AM »
i like this one.....

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7296
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Generating random numbers
« Reply #4 on: December 15, 2013, 01:00:48 PM »
Random algos generally need to be "seeded" and if you keep using the same seed number, you will keep getting the same result. The trick is to use a different seed each time you run the algo if you want different sequences each time.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Generating random numbers
« Reply #5 on: December 15, 2013, 01:19:13 PM »
the one i use is auto-seeding   :biggrin:
it doesn't re-seed on every call, but it re-seeds once every 128 to 255 calls
(the re-seed iteration count is also random)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7296
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Generating random numbers
« Reply #6 on: December 15, 2013, 02:22:26 PM »
That's fine as long as you never need a repeatable sequence.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Vortex

  • Member
  • *****
  • Posts: 2241
Re: Generating random numbers
« Reply #7 on: December 15, 2013, 08:46:31 PM »
Another one :

Code: [Select]
include     RndNumb.inc

.data

format1     db 'Random number = %d',13,10,0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses ebx

LOCAL _st:SYSTEMTIME

    invoke  GetSystemTime,ADDR _st
    movzx   eax,SYSTEMTIME.wMilliseconds[_st]
    invoke  crt_srand,eax

    mov     ebx,10
@@:
    invoke  crt_rand

    and     eax,0000000FFh ; random numbers between 0 - 255

    invoke  crt_printf,ADDR format1,eax
    dec     ebx
    jnz     @b

    ret

main ENDP

END start

Diamond Star

  • Regular Member
  • *
  • Posts: 12
Re: Generating random numbers
« Reply #8 on: December 21, 2013, 09:43:33 AM »
Thanks for everyone add thing here
I'll try them and see what the best

I appreciate your efforts all

Diamond Star

  • Regular Member
  • *
  • Posts: 12
Re: Generating random numbers
« Reply #9 on: December 27, 2013, 08:58:15 AM »
IThank you all
I find te best one is Aseed4 sample that is added by dedndave
it is good and useful

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: Generating random numbers
« Reply #10 on: December 27, 2013, 11:14:51 AM »
 :P

it does spit out good random garbage
great for games and similar applications
the older versions were a bit slower, but this one is quite fast