Author Topic: Random Float Generator  (Read 5819 times)

Adamanteus

  • Member
  • **
  • Posts: 182
    • LLC "AMS"
Random Float Generator
« on: July 04, 2012, 09:10:33 PM »
Some assembler alternative to very much scientific algos to get float random number in given range  :eusa_boohoo:

Vortex

  • Member
  • *****
  • Posts: 1734
Re: Random Float Generator
« Reply #1 on: July 05, 2012, 05:33:18 AM »
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

  • Member
  • *****
  • Posts: 7758
  • Assembler is fun ;-)
    • MasmBasic
Re: Random Float Generator
« Reply #2 on: July 05, 2012, 06:12:02 AM »
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

  • Member
  • **
  • Posts: 182
    • LLC "AMS"
Re: Random Float Generator
« Reply #3 on: July 06, 2012, 04:46:11 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

  • Member
  • *****
  • Posts: 1734
Re: Random Float Generator
« Reply #4 on: July 06, 2012, 05:55:18 AM »
Here is a quick crt_rand example :

Code: [Select]
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

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Random Float Generator
« Reply #5 on: July 06, 2012, 07:36:33 PM »
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

  • Member
  • *****
  • Posts: 1734
Re: Random Float Generator
« Reply #6 on: July 07, 2012, 05:39:44 AM »
Hi MichaelW,

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

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Random Float Generator
« Reply #7 on: July 07, 2012, 02:24:00 PM »
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.

blue_devil

  • Member
  • **
  • Posts: 86
    • SCTZine
Re: Random Float Generator
« Reply #8 on: August 05, 2012, 06:59:24 PM »
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:
SCTZine Assembly

Adamanteus

  • Member
  • **
  • Posts: 182
    • LLC "AMS"
Re: Random Float Generator
« Reply #9 on: August 24, 2012, 01:01:59 AM »
From me it's absolute  :biggrin: freeware - do anything you want, except patent as invention of you own.

blue_devil

  • Member
  • **
  • Posts: 86
    • SCTZine
Re: Random Float Generator
« Reply #10 on: August 25, 2012, 04:24:41 AM »
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:
SCTZine Assembly