True random function

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

True random function

Post 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
Last edited by Trond on Sat May 06, 2006 11:47 am, edited 1 time in total.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Cool, thanks.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: True random function

Post 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]
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: True random function

Post by Trond »

SFSxOI wrote:I can't pin down the flaws as they happen too randomly :) J/k
:lol:
troy
User
User
Posts: 51
Joined: Sat May 31, 2003 2:59 pm

Post 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.)
--
troy
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

I wouldn't say it's "true random" since it is simply using the current value from the processors clock
Image Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Well, nothing is truly random. But in multitasking environment the value of the clock cycle counter is not predictable.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Nice ASM there. Where did you learn it? I can't find any good help nor any guides :(
thanks
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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 :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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"
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Thanks for the random procedure. It's just what I needed.

cheers
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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) )
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post 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?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply