Author Topic: Generating random numbers  (Read 5632 times)

Diamond Star

• Regular Member
• Posts: 12
Generating random numbers
« on: December 15, 2013, 10:11:04 AM »
Hello
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: 11529
• Assembler is fun ;-)
Re: Generating random numbers
« Reply #1 on: December 15, 2013, 10:22:13 AM »
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: 1476
• The base type of a type is the type itself
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.incincludelib \masm32\lib\Advapi32.lib...RandomBytes proc dwLength:DWORD,pBuffer:PVOIDLOCAL 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: 8829
• Still using Abacus 2.0
Re: Generating random numbers
« Reply #3 on: December 15, 2013, 11:03:09 AM »
i like this one.....

hutch--

• Member
• Posts: 8440
• Mnemonic Driven API Grinder
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

dedndave

• Member
• Posts: 8829
• Still using Abacus 2.0
Re: Generating random numbers
« Reply #5 on: December 15, 2013, 01:19:13 PM »
the one i use is auto-seeding
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--

• Member
• Posts: 8440
• Mnemonic Driven API Grinder
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

Vortex

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

Code: [Select]
`include     RndNumb.inc.dataformat1     db 'Random number = %d',13,10,0.codestart:    call    main    invoke  ExitProcess,0main PROC uses ebxLOCAL _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    retmain ENDPEND 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

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: 8829
• Still using Abacus 2.0
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