The MASM Forum

General => The Campus => Topic started by: Diamond Star on December 15, 2013, 10:11:04 AM

Title: Generating random numbers
Post by: Diamond Star 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
Title: Re: Generating random numbers
Post by: jj2007 on December 15, 2013, 10:22:13 AM
include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  Rand()                ; initialise
  push 100
  .Repeat
        Print Str$(Rand (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1030)(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 (http://www.fourmilab.ch/random/)), and it generates a new series every time you launch the prog. And it's one of the fastest PRNGs available.
Title: Re: Generating random numbers
Post by: qWord 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
Title: Re: Generating random numbers
Post by: dedndave on December 15, 2013, 11:03:09 AM
i like this one.....
Title: Re: Generating random numbers
Post by: hutch-- 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.
Title: Re: Generating random numbers
Post by: dedndave 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)
Title: Re: Generating random numbers
Post by: hutch-- on December 15, 2013, 02:22:26 PM
That's fine as long as you never need a repeatable sequence.
Title: Re: Generating random numbers
Post by: Vortex 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
Title: Re: Generating random numbers
Post by: Diamond Star 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
Title: Re: Generating random numbers
Post by: Diamond Star 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
Title: Re: Generating random numbers
Post by: dedndave 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