Page 1 of 2

True random function

Posted: Tue May 02, 2006 6:57 pm
by Trond
Code updated for 5.20+

HyperRandom() returns a random number in the range of a long. Try to see if you can find any flaws with it.

Code: Select all

Procedure.l AbsL(Val.l)
  If Val < 0
    !neg dword [p.v_Val]
  EndIf
  ProcedureReturn Val
EndProcedure

Procedure.l HyperRandom()
  Static HyperRandomSeed
  !rdtsc
  !mov cl, ah
  !add eax, [so_HyperRandom.v_HyperRandomSeed]
  !mov [so_HyperRandom.v_HyperRandomSeed], eax
  !ror eax, cl
  ProcedureReturn
EndProcedure

OpenWindow(0, 0, 0, 512, 512, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateImage(0, 513, 513)
StartDrawing(ImageOutput(0))
  Box(0, 0, 513, 513, #White)
  For I = 0 To 512*(384/4)
    Plot(AbsL(HyperRandom() >> 22), AbsL(HyperRandom() >> 22), #Red)
  Next
StopDrawing()
ImageGadget(0, 0, 0, 512, 512, ImageID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Posted: Wed May 03, 2006 9:29 pm
by Inf0Byt3
Cool, thanks.

Re: True random function

Posted: Thu May 04, 2006 11:42 am
by SFSxOI
I can't pin down the flaws as they happen too randomly :) J/k

Thank Trond, seems to work great ! :)



[quote="Trond"]HyperRandom() returns a random number in the range of a long. Try to see if you can find any flaws with it.
quote]

Re: True random function

Posted: Thu May 04, 2006 2:30 pm
by Trond
SFSxOI wrote:I can't pin down the flaws as they happen too randomly :) J/k
:lol:

Posted: Thu May 04, 2006 4:34 pm
by troy
Trond,

Can you please explain a bit what your random algorithm is/does? for not-at-all-asm-gurus like me?

As far as I know you cannot get true random numbers without true random init (user's mouse movements, sound-card level of noise, man speech level input, etc.)

Posted: Thu May 04, 2006 5:17 pm
by Paul
I wouldn't say it's "true random" since it is simply using the current value from the processors clock

Posted: Thu May 04, 2006 7:07 pm
by Trond
Well, nothing is truly random. But in multitasking environment the value of the clock cycle counter is not predictable.

Posted: Thu May 04, 2006 8:43 pm
by josku_x
Nice ASM there. Where did you learn it? I can't find any good help nor any guides :(
thanks

Posted: Thu May 04, 2006 8:54 pm
by thefool
josku_x wrote:Nice ASM there.
Actually its not that advanced :)
But its nice use, i must say that.

RDTSC:
Read Time Stamp Counter - returns the number of clock cycles since the CPU was powered up or reset.
RDTSC reads the counter value into EDX:EAX (means BOTH edx and eax, since its a 64bit value. Eax and edx are only 32 bit so its splitted.)

Trond does some various rotation and calculation with the number to get it even more random (he uses it several times.)

As Trond stated, the output is what i would call a random number. RDTSC has other uses than this too, but this is a more creative use :)

Posted: Thu May 04, 2006 9:23 pm
by Trond
josku_x wrote:Nice ASM there. Where did you learn it? I can't find any good help nor any guides :(
thanks
Thanks. This is a nice tutorial: http://flatassembler.net/docs/tutorial.zip

Also, there is a pdf file that comes with fasm that explains the syntax all all mnemonics (that's the keywords).

I've also made a tool to get the asm output from the PB editor, very handy if you want to learn. Code:

Code: Select all

File.s = ProgramParameter()
RunProgram("c:\programfiler\PureBasic 4\Compilers\pbcompiler.exe", #DOUBLEQUOTE$ + File + #DOUBLEQUOTE$ + " /COMMENTED", "", 1)
RunProgram("c:\fasm\fasmw\fasmw", #DOUBLEQUOTE$ + "c:\programfiler\PureBasic 4\Compilers\PureBasic.asm" + #DOUBLEQUOTE$, "")
Use this parameter in the tool options (including quotes): "%TEMPFILE"

Posted: Fri May 05, 2006 9:22 pm
by rsts
Thanks for the random procedure. It's just what I needed.

cheers

Posted: Fri May 05, 2006 9:38 pm
by josku_x
@Trond thanks, I am now realizing how things work in ASM. It was first very tricky when I used TASM, but we're talking of FASM and they are very unrelated (except that they both are ASM compilers 8) )

Posted: Sat May 06, 2006 9:21 am
by Derek
If I run this with the debugger on then the output looks random but if I turn the debugger off then I can see a kind of pattern showing.

There is some diagonal banding, the bands are not solid but they are there!

Maybe not quite as random as it could be.

Posted: Sat May 06, 2006 9:47 am
by josku_x
I have a question: If I have a procedure like this:

Code: Select all

Procedure ReturnEADX()
 ProcedureReturn ; Does it return the EAX or EDX register?
EndProcedure
Does it return the content in EAX or EDX?

Posted: Sat May 06, 2006 11:47 am
by Trond
Derek wrote:If I run this with the debugger on then the output looks random but if I turn the debugger off then I can see a kind of pattern showing.

There is some diagonal banding, the bands are not solid but they are there!

Maybe not quite as random as it could be.
You're right, this is a serious flaw.

josku_X, it returns eax.