As I saw a thread mentoining the request for a minimum value for the Random function i came up with this really tiny code wchich is almost to stupid to post but because it works and is useable with - values I will show it to you:
Oh haven´t seen this. Yeah I know it is possible to improve the code but it just came to my mind and i thought maybe someone could need it.
bye Nik *shame* over this much too simple code :roll:
i = 0
Procedure RandomR(argument0, argument1)
If argument0 > argument1
min = argument1 : Else : min = argument0 : EndIf
ProcedureReturn Random(Abs(argument1-argument0))+min
EndProcedure
Procedure RandomRange(M1.l,M2.l)
If M1>M2
ProcedureReturn M2+Random(M1-M2)
EndIf
ProcedureReturn M1+Random(M2-M1)
EndProcedure
time = GetTickCount_()
For i=0 To 9000000
RandomR(234,987)
RandomR(987,234)
Next
time = GetTickCount_()-time
Debug time
time = GetTickCount_()
For i=0 To 9000000
RandomRange(234,987)
RandomRange(987,234)
Next
time = GetTickCount_()-time
Debug time
I Think this is because of Abs() but when we are talking about speed... My version is definitely the fastest and it isn´t that hard to simply use the correct values isn´t it^^
bye guys
Nik wrote:As I saw a thread mentoining the request for a minimum value for the Random function i came up with this really tiny code wchich is almost to stupid to post but because it works and is useable with - values I will show it to you:
Nik wrote:On my Machine the second Version is faster
And on all machines, Nik, but i don't know what is in some people brain.
Nik wrote:I Think this is because of Abs() but when we are talking about speed...
Not only because that, but the main reason is because he is creating 2 new variables into the procedure, and that mean more access time to main memory (i.e. RAM)
Strange. It was quite faster 4 times ( all times I tested it) here. But now it's slower and that makes more sense of course.
Yes nik's is the fastest but it seems to me like it may return both parameters, is that intentional? I though it was supposed to return the smallest number or any larger number smaller than the largest number.