Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:54:53+00:00 2026-06-08T05:54:53+00:00

I have a graph, each node have 4 child nodes. I wrote a algorithm

  • 0

I have a graph, each node have 4 child nodes. I wrote a algorithm to generate a random path from a begin node to an end node. At each node, it chooses a random next node. Visited node can be revisited.

the code is like the following:

public List<Node> GetPath(Node begin, Node end)
{
    var nodes = new List<Node>();
    var node = begin;
    while (node != end)
    {
        nodes.Add(node);
        var next = node.Children[new Random().Next(4)];
        node = next;
    }

    nodes.Add(end);

    return nodes;
}

But sometimes, the Random does not work as expected. The “new Random().Next(4)” keeps generating 0. So it is always the first child node get chose and a very long repeat sequence like node1->node2->node1->node2… is generated and eventually an out of memory exception happens.

Is there a way to make the Random class works correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:54:55+00:00Added an answer on June 8, 2026 at 5:54 am

    The reason is because Random is initialized based on the current time (there is no true random in computers… only psuedo-random). The while loop iterates too quickly, and the system time has not registered a change. So you’re re-initializing a new Random object that starts with the same value.

    Try creating one Random object that is reused throughout the method:

    public List<Node> GetPath(Node begin, Node end)
    {
        var nodes = new List<Node>();
        var node = begin;
    
        Random r = new Random();
        while (node != end)
        {
            nodes.Add(node);
            var next = node.Children[r.Next(4)];
            node = next;
        }
    
        nodes.Add(end);
    
        return nodes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a graph with heavy nodes, that is each node is
I have a graph with four nodes, each node represents a position and they
I have a weighted graph with its nodes and edges. Each node contains a
I have a list (graph?) of nodes, and each node is connected to other
I have a directed-graph, each node is a complex data type. Does anyone know
I have several nodes (undirected graph, bidirecional) ... Each two nodes have a distance...
This algorithm does a great job of traversing the nodes in a graph. Dictionary<Node,
I have an undirected graph where each node contains an array. Data can be
I have the following simple Graph class, where for each Node , I store
I have a graph in Neo4j in which nodes represent random points in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.