random number algorithm
random number algorithm
Does anyone have a randomnumber algorythm that produces better results than the one found in PB?
-
- Enthusiast
- Posts: 384
- Joined: Sat May 24, 2003 8:02 pm
- Location: Canada
- Contact:
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Why do you think the random algorithm is not sufficient?
Seems to be good and fast.
Seems to be good and fast.
Code: Select all
RandomSeed(gettickcount_())
OpenWindow(0, 0, 0, 800, 600, 0, "Random Test")
StartDrawing(WindowOutput())
Repeat
Plot(Random(800), Random(600))
ForEver
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Shot down!!Psychophanta wrote:What RGB() function do is return the three parameters as bytes of a 24bit number.
So better and faster is:
Plot(Random(800), Random(600),Random($FFFFFF))

(and Psychophanta strikes again!!! :p)
AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
Ohhh
, well try this then 
RandomSeed(gettickcount_())
OpenWindow(0, 0, 0, 800, 600, 0, "Random Test")
StartDrawing(WindowOutput())
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random($FFFFFF))
Next
Debug gettickcount_() - temp
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),RGB(Random(255),Random(255),Random(255)))
Next
Debug gettickcount_() - temp
col.l=$FFFFFF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(col))
Next
Debug gettickcount_() - temp


RandomSeed(gettickcount_())
OpenWindow(0, 0, 0, 800, 600, 0, "Random Test")
StartDrawing(WindowOutput())
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random($FFFFFF))
Next
Debug gettickcount_() - temp
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),RGB(Random(255),Random(255),Random(255)))
Next
Debug gettickcount_() - temp
col.l=$FFFFFF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(col))
Next
Debug gettickcount_() - temp
Paid up PB User !
Testing such things with debug on, can corrupt your tests...
try this:
(ps: I find it weird that the RGB test is faster than the last test..)
-Lars
try this:
Code: Select all
RandomSeed(gettickcount_())
OpenWindow(0, 0, 0, 800, 600, 0, "Random Test")
StartDrawing(WindowOutput())
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random($FFFFFF))
Next
z = gettickcount_() - temp
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),RGB(Random(255),Random(255),Random(255)))
Next
x = gettickcount_() - temp
col.l=$FFFFFF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(col))
Next
c = gettickcount_() - temp
CloseWindow(0)
OpenConsole()
PrintN(Str(z))
PrintN(Str(x))
PrintN(Str(c))
Input()
CloseConsole()
End
-Lars
AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
Still.. The second test does three calls to random (with direct values), and one call to the rgb function... The last example calls one random with a long variable, that's all..
hmm.. oh well... blame it on the excellent compiler optimizations!!
-Lars
hmm.. oh well... blame it on the excellent compiler optimizations!!

-Lars
AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
Interesting.....
$FFFFFF............661
RGB................711
Long................660
Byte................651
$FFFFFF............661
16777215.........661
Float................651
Results from this....... The float performs well !
Note: If you change the main for next loop to a repeat forever...... and stare at the screen 8O You will eventually see a picture of Elvis ! 
$FFFFFF............661
RGB................711
Long................660
Byte................651
$FFFFFF............661
16777215.........661
Float................651
Results from this....... The float performs well !

Code: Select all
RandomSeed(gettickcount_())
OpenWindow(0, 0, 0, 800, 600, 0, "Random Test")
StartDrawing(WindowOutput())
zz=1000
xx=1000
cc=1000
dd=1000
ee=1000
ff=1000
gg=1000
For b= 1 To 10
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random($FFFFFF))
Next
z = gettickcount_() - temp
If z<zz
zz=z
EndIf
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),RGB(Random(255),Random(255),Random(255)))
Next
x = gettickcount_() - temp
If x<xx
xx=x
EndIf
col.l=$FFFFFF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(col))
Next
c = gettickcount_() - temp
If c<cc
cc=c
EndIf
coll.b=$FF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(coll))
Next
d = gettickcount_() - temp
If d<dd
dd=d
EndIf
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random($FFFFFF))
Next
e = gettickcount_() - temp
If e<ee
ee=e
EndIf
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(16777215))
Next
f = gettickcount_() - temp
If f<ff
ff=f
EndIf
colll.f=$FFFFFF
temp.l=gettickcount_()
For a = 1 To 500000
Plot(Random(800), Random(600),Random(colll))
Next
g = gettickcount_() - temp
If g<gg
gg=g
EndIf
Next b
CloseWindow(0)
OpenConsole()
PrintN("$FFFFFF "+Str(zz))
PrintN("RGB "+Str(xx))
PrintN("Long "+Str(cc))
PrintN("Byte "+Str(dd))
PrintN("$FFFFFF "+Str(ee))
PrintN("16777215 "+Str(ff))
PrintN("Float "+Str(gg))
Input()
CloseConsole()
End

Paid up PB User !