RandomRange(Min.l,Max.l)

Share your advanced PureBasic knowledge/code with the community.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

RandomRange(Min.l,Max.l)

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

Post by Psychophanta »

That is done here:
viewtopic.php?t=15189
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

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

Post 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 
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post by Psychophanta »

Don't talk nonsense, nor lies please :)
Or do you still need to be explained why your one is slower? :P
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post 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
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

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

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

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply