Product(s): | OpenBuildings Generative Components | ||
Version(s): | CONNECT Edition | ||
Environment: | N\A |
How to create a set of random points confined within a range?
We can create a set of random points using the Random() function. It returns a random number in the specified range.
object Random(optional int n1, optional int n2)
If called with no arguments, returns a random double in the range 0.0 ≤ r < 1.0.
If called with one argument (n1), returns a random integer in the range 0 ≤ r < n1.
If called with two arguments (n1 and n2), returns a random integer in the range n1 ≤ r < n2.
The set of random points can be confined within a range formed by coordinates (Xmin,Ymin) and (Xmax,Ymax).
To create the points "byfunction" technique is used and below is the code.
function(int n, int Ymin, int Ymax, int Xmin, int Xmax, CoordinateSystem CS) { for(int i=0; i<n; i++) { Point PointX = new Point(this); PointX.ByCartesianCoordinates(CS, Random(Xmin,Xmax), Random(Ymin,Ymax),0); } }
The SetRandomSeed() function can also be used along Random() function. This is useful if we want the same sequence of random numbers every time.
function(int n, int Ymin, int Ymax, int Xmin, int Xmax, CoordinateSystem CS) { SetRandomSeed(100); for(int i=0; i<n; i++) { Point PointX = new Point(this); PointX.ByCartesianCoordinates(CS, Random(Xmin,Xmax), Random(Ymin,Ymax),0); } }
Please go through the sample file communities.bentley.com/.../Random-Points.gct