【问题标题】:conditions in random walk algorithm随机游走算法中的条件
【发布时间】:2013-05-13 13:53:32
【问题描述】:

我是编程初学者,我被分配了一个小项目。教练很不清楚我到底应该做什么。我所知道的是这是一种“随机游走”,我不应该使用一种方法,我只需要使用循环、分支和随机数生成器(甚至不确定这个)。它还应该从用户那里接收到一些步数,并根据给定的步数行走。 无论如何,我是一个初学者,对 C# 的了解非常有限。我在这里和许多其他地方进行了很多研究,但没有成功。代码要么非常复杂,要么使用其他语言。 我已经写了一些代码。我不知道这是否正确。 请给我一些指示。 没有错误但程序不工作,条件有问题。

 static void Main(string[] args)
    {
        int row = 100;
        int col = 100;
        int[,] matrix;
        matrix = new int[row, col];
        int n;
        Console.WriteLine("Enter the number of steps:");
        n = int.Parse(Console.ReadLine());
        const int up = 1;
        const int down = 2;
        const int left = 3;
        const int right = 4;
        int number=0;
        for (int i=0;i<100;i++)
        {
            for (int j = 0; j < 100; j++)
                matrix[i, j] = number;
        }
        Random randomdirection = new Random();
        for (int counter = 0; counter < n; counter++)
        {
            int direction = randomdirection.Next(1, 5);
            while (direction == 1)
            {
                if ((++col) < 100 && matrix[row, ++col] == 0)
                {
                    matrix[row, ++col] = ++number;
                }
                else
                    break;
            }
            while (direction == 2)
            {
                if ((--col) < 100 && matrix[row, --col] == 0)
                {
                    matrix[row, --col] = ++number;
                }
                else
                    break;
            }
            while (direction == 3)
            {
                if ((--row) < 100 && matrix[--row, col] == 0)
                {
                    matrix[--row, col] = ++number;
                }
                else
                    break;
            }
            while (direction == 4)
            {
                if ((++row) < 100 && matrix[++row,col] == 0)
                {
                    matrix[++row,col] = ++number;
                }
                else
                    break;
            }
        }
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
                Console.WriteLine(matrix[i, j]);
        }
        Console.ReadKey();
    }

这是作业: 一个酒鬼开始漫无目的地走,从灯柱开始。在每个时间步,醉汉忘记了他或她在哪里,并随机走一步,北、东、南或西,概率为 25%。 N步后酒鬼离灯柱有多远? 它非常不完整。 我们没有被教过如此合乎逻辑的课程和方法,我们不应该使用它们。我认为的重点是使用数组、循环和分支。 我确实自己编写了代码。这就是它如此混乱的原因。

改成这样: 好像我没有看到问题:((

 static void Main(string[] args)
    {
        Random randomObject = new Random();
        int randomDirection;
        int[,] mainarray;
        int row=10;
        int col=10;
        mainarray=new int[row,col];
        int number=0;
        for (int b=0;b<row;b++)
        {
            for (int c=0;c<col;c++)
                mainarray[b,c]=number;
        }

        while (true)
        {
            int n;
            Console.WriteLine("Enter the number of steps: ");
            n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                randomDirection = randomObject.Next(1, 5);
                if (randomDirection == 1 && row < 0 && row < 10)
                    mainarray[++row, col] = ++number;
                if (randomDirection == 2 && row < 0 && row < 10)
                    mainarray[--row, col] = ++number;
                if (randomDirection == 3 && col < 0 && col < 10)
                    mainarray[row, ++col] = ++number;
                if (randomDirection == 4 && col < 0 && col < 10)
                    mainarray[row, --col] = ++number;
            }
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                    Console.Write(mainarray[i,j]);
            }
        }
        Console.ReadKey();
    }

【问题讨论】:

  • 你最好解释一下你想要达到的目标
  • 你从哪里得到这个代码?你自己开始真的会更好,因为我 100% 确定你没有编写这段代码。真是一团糟……
  • I should not be using a method 是你的解释还是导师的要求? there is a problem with the conditions 条件是什么,你的random walk 逻辑要如何流动?
  • 您是否考虑过与您的导师交谈,以澄清您对语言和这项特定作业的不理解?
  • 编程是特定的。代码将完全按照您编写的代码执行。每次你运行同一个程序时,它都会做同样的事情。您不能有一个模糊的任务并尝试对其进行编码。确保计划的目标是准确的。先澄清一下。其次,当你有一个模糊的任务时,不要问我们应该怎么做。我们也不知道...

标签: c# random


【解决方案1】:

我认为你的整体方法是错误的。

假设您有一个 100 x 100 的网格。并且您想在随机方向上行走 x 步。你会想要这样的东西:

static void Main(string[] args)
{
    Random randomObject = new Random();
    int randomDirection;
    while (true) { //because your program needs to run infinitely
        Console.WriteLine("Enter the number of steps:");
        n = int.Parse(Console.ReadLine());
        //You walk a random way each step

        for (int i = 0; i < n; i++)
        {
            randomDirection = randomObject.Next(1,5);

            if (randomDirection == 1)
            {
                //walk right
            }
            if (randomDirection == 2)
            {
                //walk left
            }
            if (randomDirection == 3)
            {
                //walk up
            }
            if (randomDirection == 4)
            {
                //walk down
            }
            //Also check if you aren't walking against walls! 
        }
     }
}

在您对问题发表评论和编辑后进行编辑:

您可以像您说的那样通过randomDirection==1 &amp;&amp; row&lt;100 来检查您是否靠墙行走。但是,您必须编写一些代码才能采取其他方向,因为如果您不能那样移动,但i 会增加,那将是错误的。那你就不动了,但会算一步。

您可以按照您所说的那样执行那行代码(以及所有其他方向),以检查您是否超出界限。我会将所有 if 更改为 else if 语句。在else if (randomDirection == 4 &amp;&amp; col &lt; 100) {}之后写else { i--; }

至于检查醉汉走了多远,可以使用勾股定理。

EDIT2:您使用:randomDirection == 1 &amp;&amp; row &lt; 0 &amp;&amp; row &lt; 10。你总是检查行是否小于 0。它应该是row &gt; 0

EDIT3:实际上,您对mainArray 的全部使用毫无意义。为什么不将rowcol设置为0,如果你向右走,就做row++

static void Main(string[] args)
{
    Random randomObject = new Random();
    int randomDirection;
    int row = 1;
    int col = 1;

    while (true)
    {
        int n;
        row = 1;
        col = 1;
        Console.WriteLine("Enter the number of steps: ");
        n = Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            randomDirection = randomObject.Next(1, 4);
            if (randomDirection == 1 && row > 0 && row <= 10)
                row++;
            else if (randomDirection == 2 && row > 0 && row <= 10)
                row--;
            else if (randomDirection == 3 && col > 0 && col <= 10)
                col--;
            else if (randomDirection == 4 && col > 0 && col <= 10)
                col++;
            else
                i--;
        }
        Console.WriteLine("You have walked in row: " + row);
        Console.WriteLine("You have walked in col: " + col);
        Console.WriteLine("");
    }
}

【讨论】:

  • 问题是我不知道如何检查是否靠墙行走。我不应该在这里有一个数组吗?我的数组是正确的吗?唯一想到的就是这样写: if (randomDirection==1 && row
  • 非常感谢。你解释它的方式真的很有帮助。它现在确实有效。非常感谢。
  • @rapture 这还不完全正确,也不是解决这个问题的最佳方法,但你应该明白了 :) 祝你好运!
  • 谢谢 :) 是的,我明白了。而且我不敢相信我没有看到“row
猜你喜欢
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多