News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Random Float Generator

Started by Adamanteus, July 04, 2012, 09:10:33 PM

Previous topic - Next topic

Adamanteus

Some assembler alternative to very much scientific algos to get float random number in given range  :eusa_boohoo:

Vortex

Hi Adamanteus,

Try to combine your random number generator with Windows' system timer. On every execution of your application, you will get a different list of numbers.

jj2007

Here is a version that is compatible with a standard Masm32 installation, i.e. no environment variables required. I haven't changed anything substantial.

As to the quality of the generated numbers: you are using nrandom, which afaik has been tested a lot in the old forum, e.g. here.

Adamanteus

Quote from: Vortex on July 05, 2012, 05:33:18 AM
Try to combine your random number generator with Windows' system timer. On every execution of your application, you will get a different list of numbers.
Algorithm done for built-in whole numbers random generator, it could be crt_rand, nrandom and all others, and random seed for it is need to add by hand – crt_srnad or unknown eax value and all others   ::)

Vortex

Here is a quick crt_rand example :


include     \masm32\include\masm32rt.inc

.data

    f1      db 'Number %d = %d',13,10,0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi ebx

LOCAL _st:SYSTEMTIME

    invoke  GetSystemTime,ADDR _st
    movzx   ebx,SYSTEMTIME.wMilliseconds[_st]
@@:
    invoke  crt_rand
    dec     ebx
    jnz     @b

    mov     esi,100
    mov     ebx,10
@@:
    invoke  crt_rand
    xor     edx,edx
    div     esi
    invoke  crt_printf,ADDR f1,ebx,edx
    dec     ebx
    jnz     @b
    ret
   
main ENDP

END start

MichaelW

One potential problem with the CRT rand function is the 15-bit range.

As a test I decided to do a scatter plot of the randf_size return values. The plot showed no problems, but to make it work I had to modify randf_size.asm.

Well Microsoft, here's another nice mess you've gotten us into.

Vortex

Hi MichaelW,

Nice example. You should modify the batch file makeit.bat to build the application as GUI.

MichaelW

Hi Vortex,

I left it as a console app so that by uncommenting the printf, Sleep, and FLD ST statements it would be easy to see what is wrong.
Well Microsoft, here's another nice mess you've gotten us into.

bluedevil

Hi all,
After reading this topic, i have prepared my own random real number generator. Not perfect but gives an idea.

Also i edited your sample codes as a radasm project, i attached them

Finally, i wanna share your randf.asm and sample codes on my website, if it is not problem for you, @MichaelW, @Adamanteus ??
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

Adamanteus

From me it's absolute  :biggrin: freeware - do anything you want, except patent as invention of you own.

bluedevil

Aha thanks a lot i have been waiting for this comment :t i will publish these on my blog :greensml:
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github