How to generate random values in probable zones

Everything else that doesn't fall into one of the other PB categories.
User avatar
Psychophanta
Addict
Addict
Posts: 4976
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

How to generate random values in probable zones

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

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to generate random values in probable zones

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to generate random values in probable zones

Post by idle »

Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to generate random values in probable zones

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Psychophanta
Addict
Addict
Posts: 4976
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: How to generate random values in probable zones

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

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply