# How to select a random node [closed]

I need to know if it is possible to select a random node or select a node itself because I'm doing a zombie AI and I need him to randomly move, and I wanted to know if it's possible to do it and how.

-

## closed as not a real question by RicketNov 12 '12 at 22:03

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

If you want zombie AI, you should ask for zombie AI, rather than asking for a solution to a specific idea you have for implementing zombie AI. The answers you're getting so far to your specific question are totally correct, but are not good solutions for your broader problem. – Sean Middleditch Nov 11 '12 at 1:14
Hi and welcome to our site. Generally, questions should be really specific, and should mention what you already tried, and where you got stuck. – ashes999 Nov 11 '12 at 2:03

1. Assign a number to each possible destination node.
2. Pick a random number.
3. Select the node with the number you randomly selected.
-

In addition to @Trevor Powell answer i would say that if you are using a simple graph for your A* pathfinding (such as a grid or something), you can easily generate a random X and Y coordinate and get the node located at this point.

It could avoid assigning an id to each node.

-

Use recursion

1) Choose a random child in a node

2) Repeat until no child nodes or early escape

``````node randomnode(node currentnode)
{
int n = random number between (and including) 0 and the number of child nodes + 1
if (n == number of child nodes + 1)
{
return currentnode //early escape.
}
else
{
return randomnode(currentnode[n])
}
}
``````
-

While the previous answers are all technically correct to the question you posted, I think you're asking the wrong question.

Simply picking a random point and moving there will NOT give you good-looking zombie behavior. It's going to look and feel like a computer algorithm rather than giving the impression of "artificial intelligence." The zombie will have a tendency to make radical turns and spin on a dime to walk backwards or the like rather than aimlessly shambling like a "natural" creature (ignoring the unnaturalness of zombies, of course).

You are much better off using a wander steering behavior mixed with a random tendency to stop for a while and drool/moan/etc, or even a random speed or the like. (By random here I mean something weighted towards reasonable values, not a simple "random(min,max)" call, as again the latter will look like a computer algorithm and not feel natural.)

You can use the steering algorithms on that same site (linked because the home link on the Wander page is apparently broken) to continue making the zombies look more natural. Use obstacle avoidance rather than A* to move around objects. Maybe even use some small flocking influence to make zombies congregate or move apart from each other. The seek or pursue behaviors on the site are also great options for making zombies chase the player or other food sources. Arrival is a decent choice if you want the zombie to run the last point it saw the player but to slow down and return to normal behavior after getting there (assuming it doesn't see the player again and pick up the chase).

-
I already have a script for doing it going as you say, I just wanted to know if it was possible with A*, and all what you said the times that he will rest, moan, etc. I was sure going to implement it :) thanks you helped ^^ – Boris Wainstein Nov 11 '12 at 12:36