Page 1 of 1

random number algorithm

Posted: Wed Nov 26, 2003 6:00 am
by freewilli
Does anyone have a randomnumber algorythm that produces better results than the one found in PB?

Posted: Wed Nov 26, 2003 6:39 am
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.

Posted: Wed Nov 26, 2003 10:41 am
by Psychophanta
Does anyone have a randomnumber algorythm that produces better results than the one found in PB?
I, personally, can't imagine :!:

Posted: Thu Nov 27, 2003 10:20 pm
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

Posted: Fri Nov 28, 2003 11:07 am
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 ;)

Posted: Fri Nov 28, 2003 12:26 pm
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))

Posted: Fri Nov 28, 2003 12:30 pm
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)

Posted: Fri Nov 28, 2003 12:38 pm
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

Posted: Fri Nov 28, 2003 12:50 pm
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

Posted: Fri Nov 28, 2003 12:53 pm
by dontmailme
Maybe coz its a long ?

Posted: Fri Nov 28, 2003 12:58 pm
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

Posted: Fri Nov 28, 2003 1:04 pm
by Psychophanta
I strike again: :lol:

RandomSeed(gettickcount_()) is a surplus, coz Random() function is enough well done to need that. 8)

Posted: Fri Nov 28, 2003 1:31 pm
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: