# Is there a simple way to generate uniform points within a given region?

**URL:** https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800
**Category:** General Usage
**Tags:** gmsh
**Created:** [October 11, 2019, 11:30pm UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800 "2019-10-11T23:30:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [October 11, 2019, 11:30pm UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/1 "2019-10-11T23:30:34Z")

</div>

I’m looking for a way to generate a mesh with given region.  
Basically I want things like the Fig 1 in [https://doc.freefem.org/tutorials/poisson.html](https://doc.freefem.org/tutorials/poisson.html).

What I need are just the vertices. The triangles are not important to me.  
Is there a simple way to get this?

Thank you.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 11, 2019, 11:35pm UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/2 "2019-10-11T23:35:28Z")

</div>

You can use `rand()` to generate a random number between 0 and 1:

```julia
julia> rand()
0.45228937995996965

```

and you can generate a 2-element random vector with `rand(2)`:

```julia
julia> rand(2)
2-element Array{Float64,1}:
 0.5299598174026356 
 0.13314274980488228

```

You can generate a list of random vectors using a list comprehension:

```julia
julia> [rand(2) for _ in 1:5]
5-element Array{Array{Float64,1},1}:
 [0.931461404102448, 0.14979258911851256] 
 [0.30356330980471347, 0.3357467250045538]
 [0.5851807158436648, 0.264789888558719]  
 [0.36648327079645826, 0.730164220941981] 
 [0.4670939298076662, 0.9425081883161783] 

```

Or, if you want a more efficient solution, you can create random SVectors using StaticArrays.jl:

```julia
julia> using StaticArrays

julia> rand(SVector{2, Float64}, 5)
5-element Array{SArray{Tuple{2},Float64,1,2},1}:
 [0.926329220599331, 0.48102301661919555]  
 [0.5606673579437782, 0.017517785007929332]
 [0.9727391327916786, 0.27827197467782905] 
 [0.6926174031582435, 0.5357200141460445]  
 [0.9677270267237157, 0.8883903773952777]  

```

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [October 12, 2019, 2:14am UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/3 "2019-10-12T02:14:23Z")

</div>

Thank you. I will try this.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 12, 2019, 4:03am UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/4 "2019-10-12T04:03:52Z")

</div>

> [@rdeits](#):
>
> You can use `rand()` to generate a random number between 0 and 1:

This is probably not a good way to generate a uniform mesh, because the separation of random points will have a large variance — some points will be very close together.

One simple procedure would be to generate a uniform cartesian (or hexagonal) grid and then truncate it to the shape of interest, but that may lead to “poor” triangles (closely spaced points) at the boundaries.

In-filling a general polygonal or polyhedral shape with a nearly uniform set of points (nearly equilateral triangles) is a tricky mathematical problem that is solved by various mesh-generation algorithms. I don’t know if there is a native Julia mesh-generation program yet, but there are Julia wrappers around various external libraries that can solve this problem, e.g. [Gmsh.jl](https://github.com/JuliaFEM/Gmsh.jl) or [TRIANGLE.jl](https://github.com/cvdlab/TRIANGLE.jl).

(You can also [call Matlab](http://www.stochasticlifestyle.com/julia-ifem-1-mesh-generation-and-julias-type-system/), or [call Python](https://github.com/JuliaPy/PyCall.jl) packages like [MeshPy](https://pypi.org/project/MeshPy/) … It would be nice if we had native Julia packages for everything right away, but we’ve worked hard on inter-language calling so that you can gradually transition: take advantage of mature libraries from other languages while still writing your problem-specific code in Julia.)

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [October 12, 2019, 11:46am UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/5 "2019-10-12T11:46:57Z")

</div>

Instead of TRIANGLE.jl, consider also [TriangleMesh.jl](https://github.com/konsim83/TriangleMesh.jl). They both wrap the same C library. TRIANGLE has a nicer API(?), but TriangleMesh wraps more methods. In particular, TRIANGLE only offers (Constrained) Delaunay Triangulation, but no mesh refinement.

---

<div class="post-metadata">

### Author: ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)
#### Post date: [October 12, 2019, 11:52am UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/6 "2019-10-12T11:52:08Z")

</div>

> This is probably not a good way to generate a uniform mesh, because the separation of random points will have a large variance — some points will be very close together.

Let’s say I’m stubborn and want to generate additional points anyway, to be used as the basis of a subsequent Delaunay triangulation. Would it be better to use some specific method of generating the points, e.g. using [Sobol.jl](https://github.com/stevengj/Sobol.jl), over just uniform sampling for each coordinate?

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [December 20, 2019, 3:28pm UTC](https://discourse.julialang.org/t/is-there-a-simple-way-to-generate-uniform-points-within-a-given-region/29800/7 "2019-12-20T15:28:28Z")

</div>

Although I haven’t had time to test these, thanks for all suggestions.
