Page 1 of 1

RandomRange(Min.l,Max.l)

Posted: Thu Jun 30, 2005 2:06 pm
by Nik
Code updated for 5.20+

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:

Code: Select all

Procedure RandomRange(Min.l,Max.l)
  ProcedureReturn Min+Random(Max-Min)
EndProcedure


;test it
For i =1 To 100
  Debug RandomRange(-50,-40)
Next
bye Nik

Posted: Thu Jun 30, 2005 2:58 pm
by Psychophanta
That is done here:
viewtopic.php?t=15189

Posted: Thu Jun 30, 2005 3:19 pm
by Trond
This procedure allows you to supply the the parameters in either way (largest first or smallest first):

Code: Select all

Procedure RandomR(argument0, argument1)
  If argument0 > argument1
    min = argument1 : Else : min = argument0 : EndIf
  ProcedureReturn Random(Abs(argument1-argument0))+min
EndProcedure

Posted: Thu Jun 30, 2005 3:35 pm
by Nik
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
:oops: *shame* over this much too simple code :roll:

Posted: Thu Jun 30, 2005 3:49 pm
by Psychophanta
Trond wrote:This procedure allows you to supply the the parameters in either way (largest first or smallest first):

Code: Select all

Procedure RandomR(argument0, argument1)
  If argument0 > argument1
    min = argument1 : Else : min = argument0 : EndIf
  ProcedureReturn Random(Abs(argument1-argument0))+min
EndProcedure
Much simple and faster:

Code: Select all

Procedure RandomRange(M1.l,M2.l)
If M1>M2
  ProcedureReturn M2+Random(M1-M2)
EndIf 
ProcedureReturn M1+Random(M2-M1) 
EndProcedure 

Posted: Thu Jun 30, 2005 5:40 pm
by Trond
That's not faster, it's actually a tiny bit slower. Run this code to see it:

Code: Select all

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

Posted: Thu Jun 30, 2005 5:54 pm
by Psychophanta
Don't talk nonsense, nor lies please :)
Or do you still need to be explained why your one is slower? :P

Posted: Thu Jun 30, 2005 6:16 pm
by Nik
On my Machine the second Version is faster this is the debug output

Code: Select all

1922 
1422
I Think this is because of Abs() but when we are talking about speed... My version is definitely the fastest
:lol: and it isn´t that hard to simply use the correct values isn´t it^^
bye guys

Re: RandomRange(Min.l,Max.l)

Posted: Thu Jun 30, 2005 6:21 pm
by sec
Good trick,
Thanks.
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:

Code: Select all

Procedure RandomRange(Min.l,Max.l)
ProcedureReturn Min+Random(Max-Min)
EndProcedure


;test it
For i =1 To 100
Debug RandomRange(-50,-40)
Next
bye Nik

Posted: Thu Jun 30, 2005 6:23 pm
by Psychophanta
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)
Nik wrote:My version is definitely the fastest
:lol: of course

Posted: Thu Jun 30, 2005 7:54 pm
by Trond
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.