random number algorithm

Just starting out? Need help? Post your questions and find answers here.
freewilli
New User
New User
Posts: 6
Joined: Wed Nov 26, 2003 5:59 am

random number algorithm

Post by freewilli »

Does anyone have a randomnumber algorythm that produces better results than the one found in PB?
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Remember to use

RandomSeed(Value)

Value should be GetTickCount_(), post some code if you are getting undesireable results, we would be glad to help.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Does anyone have a randomnumber algorythm that produces better results than the one found in PB?
I, personally, can't imagine :!:
Thomas
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Apr 26, 2003 8:45 pm

Post by Thomas »

Why do you think the random algorithm is not sufficient?

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
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Change it to this :)

Plot(Random(800), Random(600),RGB(Random(255),Random(255),Random(255)))

and you could watch it for hours ;)
Paid up PB User !
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

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))
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

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))
Shot down!! :twisted:
(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
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

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
Paid up PB User !
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

Testing such things with debug on, can corrupt your tests...
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

(ps: I find it weird that the RGB test is faster than the last test..)

-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
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Maybe coz its a long ?
Paid up PB User !
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

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!! :twisted:

-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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I strike again: :lol:

RandomSeed(gettickcount_()) is a surplus, coz Random() function is enough well done to need that. 8)
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Interesting.....

$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 
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 ! :lol:
Paid up PB User !
Post Reply