---
title: "Sample random reachable navmesh points in C#"
url: https://recursiveai.org/onepath/cookbook/sample-random-reachable-navmesh-points/
description: "Pick repeatable random points inside a radius on connected navigation polygons in C#. Avoid sampling a disconnected island with OnePath."
version: "1.2.0"
---

# Sample random reachable navmesh points in C#

**OnePath** is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/sample-random-reachable-navmesh-points/sample-random-reachable-navmesh-points.zip)

Pick a repeatable random destination on connected walkable polygons.

```shell
dotnet add package RecursiveAI.OnePath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OnePath;
using RecursiveAI.OnePath.Mesh.Queries;

var rows = Enumerable.Repeat("......##......", 6);
var mesh = NavMesh.Build2D(
    Grid.FromText(string.Join("\n", rows)));
var query = new NavMeshQuery(mesh.Tiles);
var start = query.FindNearestPoly(
    new(2,0,2), new(1,1,1));
var rng = new SeededRandom(42);
var point = query.RandomPointAround(
    ref rng, start.Poly, start.Point, 10);
Console.WriteLine($"Found: {point.Found}");
if (point.Found)
    Console.WriteLine($"Point: {point.Point}");
```

```text
Found: True
Point: (3.1335903772176454, 0, 1.4717821118105765)
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/sample-random-reachable-navmesh-points/sample-random-reachable-navmesh-points.zip), then `dotnet run`.

## The full program

Each of the six grid rows is `......##......`: dots are walkable cells and hashes are blocked cells. The two islands are disconnected. The radius reaches toward the other island, but the query starts on the left and expands through connected polygons only.

```csharp
using RecursiveAI.OnePath;
using RecursiveAI.OnePath.Mesh.Queries;

var rows = Enumerable.Repeat("......##......", 6);
var mesh = NavMesh.Build2D(
    Grid.FromText(string.Join("\n", rows)));
var query = new NavMeshQuery(mesh.Tiles);
var start = query.FindNearestPoly(new(2,0,2), new(1,1,1));
var rng = new SeededRandom(42);
var point = query.RandomPointAround(
    ref rng, start.Poly, start.Point, 10);
Console.WriteLine($"Found: {point.Found}");
if (point.Found)
{
    Console.WriteLine($"Point: {point.Point}");
    Console.WriteLine($"On starting island: {point.Point.X < 6}");
}
```

```text
Found: True
Point: (3.1335903772176454, 0, 1.4717821118105765)
On starting island: True
```

Check `Found` before using the point: an invalid start, zero radius or exhausted sampling attempts returns no point.

## Variations

Copy the generator before a draw to replay the same choice. Append this to the program:

```csharp
var saved = rng;
var first = query.RandomPointAround(
    ref rng, start.Poly, start.Point, 10);
rng = saved;
var replay = query.RandomPointAround(
    ref rng, start.Poly, start.Point, 10);
Console.WriteLine(first.Found && replay.Found &&
    first.Point == replay.Point);
```

```text
True
```

## Related recipes

- [Query paths, raycasts and nearest points](https://recursiveai.org/onepath/cookbook/navmesh-queries/)
- [Rebuild a navmesh for dynamic obstacles](https://recursiveai.org/onepath/cookbook/rebuild-navmesh-for-dynamic-obstacles/)

## The short answer

Find the starting polygon, create a `SeededRandom`, then call `NavMeshQuery.RandomPointAround` with that polygon, the center and the radius. Test the result’s `Found` property before using `Point`. It samples the portions of reachable polygons inside the circle instead of choosing arbitrary world coordinates and snapping them afterward.

The sample uses two islands so connectivity matters. Sampling all walkable polygons could select the other island even though it is close in straight-line distance. Starting the circle search from the left island excludes that disconnected surface.

## How do you get a random point on a navmesh?

There are two useful scopes. `RandomPoint` samples every polygon accepted by the filter across the mesh. `RandomPointAround` first finds polygons through a circle search from a starting polygon and samples within the circle. Choose the latter for local wandering from a known position; choose the former when connectivity to the current character does not matter.

The [query documentation](https://recursiveai.org/onepath/docs/navmesh-queries/) specifies area weighting. A larger polygon receives proportionally more weight than a smaller one, measured by its X/Z outline area. Picking a polygon index uniformly would not have that property and would bias destinations toward finely divided parts of the mesh.

The returned height lies on the selected polygon’s surface. On stacked geometry, each found polygon counts separately. The query is not a method for choosing only the visually topmost floor under a point.

## What is a random reachable point in a radius?

In this API, the circle search traverses connected polygons through portals that touch the circle. The final point lies within the circle in X/Z. The radius is not a path-length allowance: a winding route through those polygons can be longer than the straight-line distance from the center.

Use a start polygon associated with the character’s actual walkable surface. `FindNearestPoly` searches within supplied half-extents; if it finds no acceptable polygon, the reference is invalid and the random query returns no point. On multiple floors, choose vertical extents that do not accidentally snap the character to a different surface.

The sampler clips found polygons to the circle’s bounding square, draws from those clipped areas and rejects points outside the circle. After 64 failed attempts it returns no point. Zero radius and an invalid start also produce no point. A failed draw is a result to handle, not a default coordinate to send a character toward.

## How do you make random navmesh movement repeatable?

Keep a `SeededRandom` instance and pass it by reference. Its state advances as the sampler draws. Recreating it with the same seed before every request repeats the first choice instead of producing a sequence. Copying and restoring the state repeats subsequent draws, as the variation demonstrates.

Repeatability also requires the same mesh, filter, start, center, radius and call sequence. Changing a door’s walkability changes which polygons can be reached; restoring only the random state cannot restore that geometry. Reacquire the start polygon after tile replacements because old references can become stale.

For a wandering character, retain the generator between destination requests, accept only successful samples, and then request a path to the chosen point. Keep “no destination found” separate from a legitimate destination at the coordinate origin. This avoids accidental movement caused by reading the result’s default `Point` when `Found` is false.
