PB 6.20 - UserLibrary - Random extension
Posted: Thu Dec 12, 2024 6:37 pm
Dear all,
with the update of PB 6.20 we have the ability to compile our own user libraries for PureBasic.
PureBasic 6.20 beta 1 is out !
Here is a very small use-case example, how a user library for an extension of the Random function could look like:
After compiling the user library you can use such functions in normal PB codes:
with the update of PB 6.20 we have the ability to compile our own user libraries for PureBasic.
PureBasic 6.20 beta 1 is out !
Here is a very small use-case example, how a user library for an extension of the Random function could look like:
Code: Select all
DisablePureLibrary Random
CompilerIf #PB_Compiler_32Bit
#RandomMax = $7FFFFFFE
#RandomMaxInverse = 4.6566128774142012721e-10
CompilerElse
#RandomMax = $7FFFFFFFFFFFFFFE
#RandomMaxInverse = 1.0842021724855044342e-19
CompilerEndIf
; QuickHelp RandomFloat([Maximum [, Minimum]) - Return a random float between 0.0 (or Minimum) and 1.0 (or Maximum)
ProcedureDLL.f RandomFloat3(Maximum.f, Minimum.f)
ProcedureReturn (Maximum-Minimum) * #RandomMaxInverse * Random(#RandomMax) + Minimum
EndProcedure
ProcedureDLL.f RandomFloat2(Maximum.f)
ProcedureReturn Maximum * #RandomMaxInverse * Random(#RandomMax)
EndProcedure
ProcedureDLL.f RandomFloat()
ProcedureReturn #RandomMaxInverse * Random(#RandomMax)
EndProcedure
; QuickHelp RandomDouble([Maximum [, Minimum]) - Return a random double between 0.0 (or Minimum) and 1.0 (or Maximum)
ProcedureDLL.d RandomDouble3(Maximum.d, Minimum.d)
ProcedureReturn (Maximum-Minimum) * #RandomMaxInverse * Random(#RandomMax) + Minimum
EndProcedure
ProcedureDLL.d RandomDouble2(Maximum.d)
ProcedureReturn Maximum * #RandomMaxInverse * Random(#RandomMax)
EndProcedure
ProcedureDLL.d RandomDouble()
ProcedureReturn #RandomMaxInverse * Random(#RandomMax)
EndProcedure
; QuickHelp RandomAngle() - Return a random angle between 0.0 and 2π
ProcedureDLL.d RandomAngle()
ProcedureReturn 2*#PI*#RandomMaxInverse * Random(#RandomMax+1) ; exclude 2π, because it's 0.0
EndProcedure
; QuickHelp RandomSign() - Return randomly -1 or 1
ProcedureDLL.i RandomSign()
ProcedureReturn Random(1)*2 - 1
EndProcedure
; QuickHelp RandomCauchy([Width [, Center]) - Return a random double which value is Cauchy-distributed
ProcedureDLL.d RandomCauchy3(Width.d, Center.d)
Protected X.d = RandomDouble3(1.0, -1.0)
ProcedureReturn Tan(#PI*X)*Width + Center
EndProcedure
ProcedureDLL.d RandomCauchy2(Width.d)
Protected X.d = RandomDouble3(1.0, -1.0)
ProcedureReturn Tan(#PI*X)*Width
EndProcedure
ProcedureDLL.d RandomCauchy()
Protected X.d = RandomDouble3(1.0, -1.0)
ProcedureReturn Tan(#PI*X)
EndProcedure
; QuickHelp RandomGaussian([Width [, Center]) - Return a random double value which is Gaussian-distributed
ProcedureDLL.d RandomGaussian3(Width.d, Center.d)
Protected X.d = RandomFloat3(1.0, -1.0)
Protected Ln.d = Log(1-X*X)*0.5
ProcedureReturn Sign(X)*Sqr(Sqr((4.546884979448284327+Ln)*(4.546884979448284327+Ln)-Ln*14.28446044815250805)-4.546884979448284327-Ln)*Width + Center
EndProcedure
ProcedureDLL.d RandomGaussian2(Width.d)
ProcedureReturn RandomGaussian3(Width, 0.0)
EndProcedure
ProcedureDLL.d RandomGaussian()
ProcedureReturn RandomGaussian3(1.0, 0.0)
EndProcedure
Code: Select all
Define I.i
For I = 1 To 10
Debug RandomDouble()
;Debug RandomDouble(10.0, -10.0)
;Debug RandomGaussian()
;Debug Degree(RandomAngle())
Next
0.0361703154404072607652765
0.4012873531735324239555496
0.6739581408816760488278419
0.107140085012712382717126
0.7553493409195806140843388
0.2252100825398675454369624
0.9709403297162259027786035
0.1601054234211059801307186
0.5367853854606794428150351
0.4389879952397071072311974