Page 1 of 1

How to generate random values in probable zones

Posted: Tue Oct 17, 2017 3:35 pm
by Psychophanta
Hi all here.

I am trying to write a procedure which accepts a math function or any other kind of data to tell to the procedure the zone (in the real rect) in which the random generator must generate results with more or less probabilities.

In other words, i explain:
Random() native PB function is able to generate numbers (scalars) inside a defined section in the real rect. But:
How to generate it inside the the same section but given a curve (math function -for example Gauss function- ) where the generated numbers are most probable there where the curve is higher and less probable there where the curve is lower?

Thanks in advance!
:)

Re: How to generate random values in probable zones

Posted: Tue Oct 17, 2017 4:22 pm
by mk-soft
Perhaps over natural logarithm

Code: Select all

Dim c(100)

Define r.f

For i = 1 To 100000
  r = Random(1700) * 0.001 + 1.0
  v = Log(r) * 100
  c(v) + 1
Next
For i = 0 To 100
  Debug "Count of " + i + " = " + c(i)
Next

Re: How to generate random values in probable zones

Posted: Tue Oct 17, 2017 9:37 pm
by idle

Re: How to generate random values in probable zones

Posted: Tue Oct 17, 2017 10:23 pm
by STARGĂ…TE
here are also two procedure for random numbers with Gaussian or Cauchy distribution.

Code: Select all

Procedure.f RandomGaussian(Width.f=1.0)
	
	Protected X.f = 9.3132257461547851563e-10 * (Random(2147483646)-1073741823)
	Protected Ln.f = Log(1-X*X)*0.5
	
	ProcedureReturn Sign(X)*Sqr(Sqr((4.546884979448284327+Ln)*(4.546884979448284327+Ln)-Ln*14.28446044815250805)-4.546884979448284327-Ln)*Width
	
EndProcedure



Procedure.f RandomCauchy(Width.f=1.0)
	
	Protected X.f = 9.3132257461547851563e-10 * (Random(2147483646)-1073741823)
	
	ProcedureReturn Tan(#PI*X)*Width
	
EndProcedure
The transformation rule (for any distributions) is, you have to integrate your function, build the inverse function of it, and than you can apply it on Random().
Function: Cauchy(x) = 1/pi * s/(s^2+x^2)
Integral: 0.5 + arcTan(x/s)/pi
Inverse: -s * cot(pi*x) or s*tan(pi*x) if you cange the definition range of x

Re: How to generate random values in probable zones

Posted: Wed Oct 18, 2017 6:35 am
by Psychophanta
Thank you all.
I guess with your points i'll have the idea to build some nice approaches and then a good solid procedure for it. :)