# Finding a random node?

Hello,
I was wondering how to get a Vector3 position of a random node on a specific graph. If this can be done, is there a way to get a random node from a specific area of that graph (an area being a group of nodes of a specific color within that graph itself after the graph scans)? What I am referring to is when after the graph scans, there are groups of nodes in that graph that have unique colors inside shapes of surrounding colliders that close off each shape (forming an ‘area’ inside that shape). Is there also a way to determine WHAT area the seeker is located in, pick a random node WITHIN that area that the seeker is inside, and have the seeker move to a node that is inside THAT area?

``````As for my question on 'one-way' path finding, how can THIS be done? What if I have normal 2-way roads with cars going in one direction on each of the sides of the roads (with a median separating the middle of each of the roads), and the seeker needs to find a way to get to the left side of the intersecting road (specifically on the particular side of the intersecting road that is currently closest TO the seeker just before the seeker finally makes its left turn onto the intersecting road). There would THEN need to be a calculation that AFTER the seeker makes its left turn, it would need to make a u-turn to get to THAT particular side of the road. In 2-way path finding, the seeker would automatically make its left hand turn onto the wrong side of the intersecting road (against traffic). Thank you for any information.

Sincerely,
Michael S. Lowe``````

Hi

The simple stupid but correct code for this is:
`var node = AstarPath.active.GetNearest (transform.position, NNConstraint.Default).node; if (node != null) { var reachable = PathUtilities.GetReachableNodes (node); var randomPointOnARandomNode = PathUtilities.GetPointsOnNodes (reachable, 1); }`

This is however a bit slow since it needs to find all reachable nodes (which can be a lot).

If you only want a random point, but not necessarily with a uniform distribution over the graph (some nodes might be more likely than others to be picked).

`
// Get a random point
var randomPosition = transform.position + Random.insideUnitSphere*100;

// Find the node the character is standing on
var node = AstarPath.active.GetNearest (transform.position, NNConstraint.Default).node;

// Configure an NNConstraint
// which makes sure the resulting node can be reached from your character
var constraint = NNConstraint.Default;
constraint.constrainArea = true;
constraint.area = node.area;
var closestOnGraph = AstarPath.active.GetNearest (randomPosition, constraint);
var closestPoint = closestOnGraph.clampedPosition;
var closestNode = closestOnGraph.node;
`

As for one way path finding.
I am sorry to say that I think a more specialised pathfinding library is a better fit for that. The A* Pathfinding Project tries to be applicable in a wide variety of cases, but pathfinding for cars on roads needs very specialised logic to work well.
I mean, you can probably do it with a point graph and unidirectional links, but I don’t think it will be worth it.