**random_data**,
a MATLAB code which
uses a random number generator (RNG) to sample points for
various probability distributions, spatial dimensions, and geometries,
including the M-dimensional cube, ellipsoid, simplex and sphere.

Most of these routines assume that there is an available source of pseudorandom numbers, distributed uniformly in the unit interval [0,1]. In this package, that role is played by the routine R8_UNIFORM_01(), which allows us some portability. We can get the same results in C, FORTRAN or MATLAB, for instance. In general, however, it would be more efficient to use the language-specific random number generator for this purpose.

If we have a source of pseudorandom values in [0,1], it's trivial to generate pseudorandom points in any line segment; it's easy to take pairs of pseudorandom values to sample a square, or triples to sample a cube. It's easy to see how to deal with square region that is translated from the origin, or scaled by different amounts in either axis, or given a rigid rotation. The same simple transformations can be applied to higher dimensional cubes, without giving us any concern.

For all these simple shapes, which are just generalizations of
a square, we can easily see how to generate sample points that
we can guarantee will lie inside the region; in most cases, we
can also guarantee that these points will tend to be *uniformly
distributed*, that is, every subregion can expect to contain
a number of points proportional to its share of the total area.

However, we will **not** achieve uniform distribution in the
simple case of a rectangle of nonequal sides **[0,A]** x **[0,B]**,
if we naively scale the random values **(u1,u2)** to
**(A*u1,B*u2)**. In that case, the expected point density of
a wide, short region will differ from that of a narrow tall region.
The absence of uniformity is most obvious if the points are plotted.

If you realize that uniformity is desirable, and easily lost, it is possible to adjust the approach so that rectangles are properly handled.

But rectangles are much too simple. We are interested in circles, triangles, and other shapes. Once the geometry of the region becomes more "interesting", there are two common ways to continue.

In the *acceptance-rejection method*,
uniform points are generated in a superregion that encloses the
region. Then, points that do not lie within the region are rejected.
More points are generated until enough have been accepted to satisfy the
needs. If a circle was the region of interest, for instance, we
could surround it with a box, generate points in the box, and throw
away those points that don't actually lie in the circle. The resulting
set of samples will be a uniform sampling of the circle.

In the *direct mapping* method, a formula or mapping
is determined so that each time a set of values is taken from
the pseudorandom number generator, it is guaranteed to correspond
to a point in the region. For the circle problem, we can use
one uniform random number to choose an angle between 0 and 2 PI,
the other to choose a radius. (The radius must be chosen in
an appropriate way to guarantee uniformity, however.) Thus,
every time we input two uniform random values, we get a pair
(R,T) that corresponds to a point in the circle.

The acceptance-rejection method can be simple to program, and can handle arbitrary regions. The direct mapping method is less sensitive to variations in the aspect ratio of a region and other irregularities. However, direct mappings are only known for certain common mathematical shapes.

Points may also be generated according to a nonuniform density. This creates an additional complication in programming. However, there are some cases in which it is possible to use direct mapping to turn a stream of scalar uniform random values into a set of multivariate data that is governed by a normal distribution.

Another way to generate points replaces the uniform pseudorandom number
generator by a *quasirandom number generator*. The main difference
is that successive elements of a quasirandom sequence may be highly
correlated (bad for certain Monte Carlo applications) but will tend
to cover the region in a much more regular way than pseudorandom
numbers. Any process that uses uniform random numbers to carry out
sampling can easily be modified to do the same sampling with
a quasirandom sequence like the Halton sequence, for instance.

The library includes a routine that can write the resulting data points to a file.

The computer code and data files made available on this web page are distributed under the MIT license

**random_data** is available in
a C version and
a C++ version and
a FORTRAN90 version and
a MATLAB version and
a Python version.

asa183, a MATLAB code which implements the Wichman-Hill pseudorandom number generator.

ball_grid, a MATLAB code which computes grid points that lie inside a ball.

disk_grid, a MATLAB code which computes grid points that lie inside a disk.

histogram_data_2d_sample, a MATLAB code which demonstrates how to construct a Probability Density Function (PDF) from a frequency table over a 2D domain, and then to use that PDF to create new samples.

histogram_pdf_sample, a MATLAB code which demonstrates how sampling can be done by starting with the formula for a PDF, creating a histogram, constructing a histogram for the CDF, and then sampling.

histogram_pdf_2d_sample, a MATLAB code which demonstrates how uniform sampling of a 2D region with respect to some known Probability Density Function (PDF) can be approximated by decomposing the region into rectangles, approximating the PDF by a piecewise constant function, constructing a histogram for the CDF, and then sampling.

ring_data , a MATLAB code which creates, plots, or saves data generated by sampling a number of concentric, possibly overlapping rings.

sammon_data, a MATLAB code which generates six sets of M-dimensional data for cluster analysis.

simplex_coordinates, a MATLAB code which computes the Cartesian coordinates of the vertices of a regular simplex in M dimensions.

tetrahedron_grid, a MATLAB code which computes a tetrahedral grid of points.

tetrahedron_monte_carlo, a MATLAB code which uses the Monte Carlo method to estimate integrals over a tetrahedron.

tetrahedron_samples, a dataset directory which contains examples of sets of sample points from the unit tetrahedron.

triangle_grid, a MATLAB code which computes a triangular grid of points.

triangle_histogram, a MATLAB code which computes histograms of data on the unit triangle.

triangle_monte_carlo, a MATLAB code which uses the Monte Carlo method to estimate integrals over a triangle.

triangle_samples, a dataset directory which contains examples of sets of sample points from the unit triangle.

uniform, a MATLAB code which samples the uniform random distribution.

- brownian.m creates Brownian motion points.
- dpo_fa.m factors a real symmetric positive definite matrix.
- dpo_sl.m solves a linear system factored by DPO_CO or DPO_FA.
- direction_uniform_nd.m generates a random direction vector.
- ksub_random2.m selects a random subset of size K from a set of size N.
- normal_circular.m creates circularly normal points.
- normal_square.m creates normally distributed points.
- polygon_centroid.m computes the centroid of a polygon.
- stri_angles_to_area.m, computes the area of a spherical triangle;
- stri_sides_to_angles.m, computes the angles of a spherical triangle from its sides;
- stri_vertices_to_sides.m, computes the sides of a spherical triangle from its sides;
- triangle_area.m computes the area of a triangle.
- uniform_in_annulus.m returns uniform random points inside an annulus.
- uniform_in_annulus_sector.m returns uniform random points inside an annular sector.
- uniform_in_circle.m returns uniform random points inside the unit circle.
- uniform_in_ellipse.m returns uniform random points inside an ellipse.
- uniform_in_ellipsoid.m returns uniform random points inside an ellipsoid.
- uniform_in_hexagon.m returns uniform random points inside a unit hexagon.
- uniform_in_hypercube.m returns uniform random points inside the unit hypercube.
- uniform_in_hypersphere.m returns uniform random points inside the unit hypersphere.
- uniform_in_parallelogram.m returns uniform random points inside a parallelogram.
- uniform_in_polygon.m returns uniform random points inside a polygon.
- uniform_in_sector.m returns uniform random points inside a circular sector.
- uniform_in_simplex.m returns uniform random points inside the unit simplex.
- uniform_in_tetrahedron.m returns uniform random points inside a tetrahedron.
- uniform_in_triangle.m returns uniform random points inside an arbitrary triangle.
- uniform_on_circle.m returns uniform random points on the surface of a circle.
- uniform_on_ellipse.m returns uniform random points on the surface of an ellipse.
- uniform_on_ellipsoid.m returns uniform random points on the surface of an ellipsoid.
- uniform_on_hemisphere_phong.m returns uniform random points on the surface of the unit hemisphere, with the Phong distribution.
- uniform_on_hypercube.m returns uniform random points on the surface of the unit hypercube.
- uniform_on_hypersphere.m returns uniform random points on the surface of the unit hypersphere.
- uniform_on_simplex.m returns uniform random points on the surface of the unit simplex.
- uniform_on_sphere_patch_tp.m returns uniform random points on the surface of a unit sphere (THETA,PHI) patch in 3D.
- uniform_on_sphere_patch_xyz.m returns uniform random points on the surface of a unit sphere XYZ patch in 3D.
- uniform_on_sphere_triangle_xyz.m returns uniform random points on the surface of a spherical triangle on the unit sphere, using XYZ coordinates.
- uniform_on_triangle.m returns uniform random points on the surface of a triangle.
- uniform_walk.m generates points on a uniform random walk.