-1

I want to generate a number of random points in hexagon. To do so, i generate random points in square and then try to use conditions to drop not suitable pairs. I tried solutions like this:

import scipy.stats as sps
import numpy as np
size=100
kx = 1/np.sqrt(3)*sps.uniform.rvs(loc=-1,scale=2,size=size)
ky = 2/3*sps.uniform.rvs(loc=-1,scale=2,size=size)
pairs = [(i, j) for i in kx for j in ky]
def conditions(pair):
    return (-1/np.sqrt(3)<pair[0]<1/np.sqrt(3)) & (-2/3<pair[1]<2/3)
mask = np.apply_along_axis(conditions, 1, pairs)
hex_pairs = np.extract(mask, pairs)
L=len(hex_pairs)
print(L)

In this example I try to construct a logical mask for future use of np.extract to extract needed values. I try to apply conditional function to all pairs from a list. But it seems that I understand something badly because if using this mask the output of this code is:

10000

That means that no pairs were dropped and all boolean numbers in mask were True. Can anyone suggest how to correct this solution or maybe to put it another way (with a set of randomly distributed points in hexagon as a result)?

goroshek
  • 1
  • 1
  • I think your initially created pairs simply fulfil the condition. Also: how does you condition relate to a hexagon? – Thomas Hilger Jun 10 '22 at 17:31
  • I think a simple way to get a result is to create a hexagonal polygon, create uniform random numbers in an area which encloses this hexagon and then just use one of the libraries for polygons to check for the points being inside of this polygon. https://stackoverflow.com/a/36400130/7084566 – Thomas Hilger Jun 10 '22 at 17:35
  • Thank you so much, I was really off when I asked this question lol. def conditions(pair): return (pair[1] < 2/3 -np.abs(pair[0])/np.sqrt(3)) & (pair[1] > -2/3 + np.abs(pair[0])/np.sqrt(3) ) corrected the situation and seems like working (7555 pairs remained live). Really bad things with mine attentiveness – goroshek Jun 10 '22 at 17:37
  • Also thanks for the link, I will definitely look at it and try to code it another way! If you make an answer i will upvote it – goroshek Jun 10 '22 at 17:40

1 Answers1

0

The reason why none of your pairs gets eliminated is, that they are created such that the condition is fulfilled (all x-values are in [-1/sqrt(3), 1/sqrt(3)], similar for the y-values).

I think an intuitive and easy way to get their is to create a hexagonal polygon, generate uniformly distributed random numbers within a square that encloses this hexagon and then apply the respective method from one of the already existing polygon-libraries, such as shapely. See e.g. https://stackoverflow.com/a/36400130/7084566

Thomas Hilger
  • 456
  • 3
  • 11