Generate random locations within a triangular domain

前端 未结 3 846
野趣味
野趣味 2020-12-18 10:36

I want to generate x and y having a uniform distribution and limited by [xmin,xmax] and [ymin,ymax]

The points (x

3条回答
  •  囚心锁ツ
    2020-12-18 11:29

    Uniform on the triangle?

    import numpy as np
    
    N = 10 # number of points to create in one go
    
    rvs = np.random.random((N, 2)) # uniform on the unit square
    # Now use the fact that the unit square is tiled by the two triangles
    # 0 <= y <= x <= 1 and 0 <= x < y <= 1
    # which are mapped onto each other (except for the diagonal which has
    # probability 0) by swapping x and y.
    # We use this map to send all points of the square to the same of the
    # two triangles. Because the map preserves areas this will yield 
    # uniformly distributed points.
    rvs = np.where(rvs[:, 0, None]>rvs[:, 1, None], rvs, rvs[:, ::-1])
    
    Finally, transform the coordinates
    xmin, ymin, xmax, ymax = -0.1, 1.1, 2.0, 3.3
    rvs = np.array((ymin, xmin)) + rvs*(ymax-ymin, xmax-xmin)
    

    Uniform marginals? The simplest solution would be to uniformly concentrate the mass on the line (ymin, xmin) - (ymax, xmax)

    rvs = np.random.random((N,))
    rvs = np.c_[ymin + (ymax-ymin)*rvs, xmin + (xmax-xmin)*rvs]
    

    but that is not very interesting, is it?

提交回复
热议问题