Generate random points on the surface of the cylinder

I want to create random points on the surface of the cylinder so that the distance between the points falls in the range of 230 and 250. I used the following code to generate random points on the surface of the cylinder:

import random,math
H=300
R=20
s=random.random()
#theta = random.random()*2*math.pi
for i in range(0,300):
    theta = random.random()*2*math.pi
    z = random.random()*H
    r=math.sqrt(s)*R
    x=r*math.cos(theta)
    y=r*math.sin(theta)
    z=z
    print 'C'  , x,y,z

How can I generate random points so that they fall in a range (on the surface of the cylinder)?

+5
source share
2 answers

, , . "" w=2*pi*r h, . , " " - .

, "", (x1-x2) (w -x1 + x2) - , , .

, @VincentNivoliers , h = 300 r = 20 , .

+1

, , . , , , , , . , , .

, , - . , . :

  • - , , . , , .

  • , , , -, . , .

:

from __future__ import division
import numpy as np

r, h = 20, 300
w = 2*np.pi*r
int_w = int(np.rint(w))
mult = 10
pdf = np.ones((h*mult, int_w*mult), np.bool)
points = []
min_d, max_d = 230, 250

available_locs = pdf.sum()
while available_locs:
    new_idx = np.random.randint(available_locs)
    new_idx = np.nonzero(pdf.ravel())[0][new_idx]
    new_point = np.array(np.unravel_index(new_idx, pdf.shape))
    points += [new_point]
    min_mask = np.ones_like(pdf)
    if max_d is not None:
        max_mask = np.zeros_like(pdf)
    else:
        max_mask = True
    for p in [new_point - [0, int_w*mult], new_point +[0, int_w*mult],
              new_point]:
        rows = ((np.arange(pdf.shape[0]) - p[0]) / mult)**2
        cols = ((np.arange(pdf.shape[1]) - p[1]) * 2*np.pi*r/int_w/mult)**2
        dist2 = rows[:, None] + cols[None, :]
        min_mask &= dist2 > min_d*min_d
        if max_d is not None:
            max_mask |= dist2 < max_d*max_d
    pdf &= min_mask & max_mask
    available_locs = pdf.sum()
points = np.array(points) / [mult, mult*int_w/(2*np.pi*r)]

, , . , .

min_d, max_d = 50, 200

, 5 : enter image description here

, , , - .

+1

All Articles