This may have legs in it yet, though it may be effected by the clock frequency between systems regarding the magnitude of the shifts.
It's looking random on my system and it's reasonably fast, though you may see angled bands appearing.
first and second codes
left side RDTSC gen
right side Random()
third code back to RandomBit and RandomBuffer
I'm starting to think that with a bit of tweaking this could possibly pass the Nist tests as a TRNG
Enable ASM in options
Disable debugger
Code:
EnableExplicit
Global w,h,hdc
Global rVal,bset
Procedure.i RandomNum(num,Range)
RDTSC ;Read the CPU Time Stamp Counter
SHL eax, 6 ; << 5
XOR eax, num ;xor result
SHR eax, 2 ; >> 2
MOV ebx, Range ;do test for odd or even
AND ebx,1
AND ebx,ebx
JE ED ;
MOV ebx, Range
XOR edx,edx ;do mod zero edx
DIV ebx ;num % Range
INC edx
MOV eax,edx
! ED:
MOV ebx, Range ;get val
INC ebx ;val +1
XOR edx,edx ;do mod zero edx
DIV ebx ;num % Range
MOV eax,edx ;
DEC eax ;num - 1
ProcedureReturn
EndProcedure
Procedure RRandom(Range)
If bset = 0
Protected a
While a < 3
rval = RandomNum(rval,Range)
a+1
Wend
bset = 1
Else
rval = RandomNum(rval,Range)
EndIf
ProcedureReturn rval
EndProcedure
Procedure draw(hdc)
Protected a,x,y,st,et,Strt.s
Strt = "Random Test "
st = GetTickCount_()
While a < w*h
x = rRandom(w/2)
y = rRandom(h)
SetPixel_(hdc,x,y,RGB(0,0,0))
a+1
Wend
et = GetTickCount_()-st
strt + "RDTSC " + Str(et) + " "
SetWindowTitle(0,Strt)
a = 0
st = GetTickCount_()
While a < w*h
x = Random(w/2)+w/2
y = Random(h)
SetPixel_(hdc,x,y,RGB(0,0,0))
a+1
Wend
et = GetTickCount_()-st
strt + " Random " + Str(et)
SetWindowTitle(0,strt)
EndProcedure
w=640
h=480
OpenWindow(0,0,0,w,h,"Random Test",#PB_Window_WindowCentered| #PB_Window_SystemMenu | #PB_Window_TitleBar)
hdc = GetDC_(WindowID(0))
SetWinBackgroundColor(WindowID(0),RGB(255,255,255))
Delay(100)
CreateThread(@draw(),hdc)
Repeat
Until WaitWindowEvent() = 16
looks a bit blotchy on the left!
Code:
EnableExplicit
Global w,h,hdc
Global rVal,bset
Procedure.i RandomNum(num,Range)
RDTSC ;Read the CPU Time Stamp Counter
SHL eax, 6 ; << 5
XOR eax, num ;xor result
SHR eax, 2 ; >> 2
MOV ebx, Range ;do test for odd or even
AND ebx,1
AND ebx,ebx
JE ED ;
MOV ebx, Range
XOR edx,edx ;do mod zero edx
DIV ebx ;num % Range
INC edx
MOV eax,edx
! ED:
MOV ebx, Range ;get val
INC ebx ;val +1
XOR edx,edx ;do mod zero edx
DIV ebx ;num % Range
MOV eax,edx ;
DEC eax ;num - 1
ProcedureReturn
EndProcedure
Procedure RRandom(Range)
If bset = 0
Protected a
While a < 3
rval = RandomNum(rval,Range)
a+1
Wend
bset = 1
Else
rval = RandomNum(rval,Range)
EndIf
ProcedureReturn rval
EndProcedure
Procedure draw(hdc)
Protected a,x,y,st,et,Strt.s,*px.long,offset,pitch
Strt = "Random Test "
SetFrameRate(25)
While 1
StartDrawing(ScreenOutput())
hdc= DrawingBuffer()
Pitch = DrawingBufferPitch()
a=0
While a < w/2*h/2
x = rRandom(w/2)
y = rRandom(h)
offset = (x*4 + (y * pitch))
*px = hdc + offset
*px\l = rRandom(16777215)
x = Random((w)/2)+w/2
y = Random(h)
offset = (x*4 + (y * pitch))
*px = hdc + offset
*px\l = Random(16777215)
a+1
Wend
StopDrawing()
FlipBuffers()
ClearScreen(RGB(0,0,0))
Wend
EndProcedure
w=640
h=480
InitSprite()
OpenWindow(0,0,0,w,h,"Random Test",#PB_Window_WindowCentered| #PB_Window_SystemMenu | #PB_Window_TitleBar)
OpenWindowedScreen(WindowID(0),0,0,w,h,0,0,0)
w-1
h-1
hdc = GetDC_(WindowID(0))
SetWinBackgroundColor(WindowID(0),RGB(255,255,255))
Delay(100)
CreateThread(@draw(),hdc)
Repeat
Until WaitWindowEvent() = 16
And back to RandomBit() and RandomBuffer()
haven't looked at the results from these yet
Code:
Procedure SetBit(*num,bit)
Protected of.i
of = bit >> 3
PokeB(*num+of,(PeekB(*num+of) | (1 << (bit % 8))))
EndProcedure
Procedure GetBit(*buffer,index)
Protected mf
mf = (index % 8)
ProcedureReturn (PeekB(*buffer+(index>>3)) & (1 << mf)) >> mf
EndProcedure
Procedure.i RandomBit()
Static num
RDTSC ;Read the CPU Time Stamp Counter
SHL eax, 6 ; << 5
XOR eax, num ;xor result
SHR eax, 2 ; >> 2
MOV num,eax
AND eax, $1
ProcedureReturn
EndProcedure
Procedure RandomBuffer(*buffer,size)
Protected n.f, num, a,bit
Static bset
If bset = 0
While a < 3
Randombit()
a+1
Wend
bset = 1
EndIf
For a = 1 To size*8
bit=RandomBit()
If bit
setbit(*buffer,a)
EndIf
Next
EndProcedure
Define bit.i, num.i,a.i,b.i
For b = 1 To 100 ;get a random bit
Debug Randombit()
Next
*rbuf = AllocateMemory(1024) ;fill a buffer
randombuffer(*rbuf,1024)
For a = 0 To 1024*8 ;get bits
Debug getbit(*rbuf,a)
Next
a = 0
While a < 1024
Debug (PeekB(*rbuf+a) & $F0)
a+1
Wend