Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 让两个变量中的一个随机拥有两个选项中的一个更简单的方法?_C#_Random - Fatal编程技术网

C# 让两个变量中的一个随机拥有两个选项中的一个更简单的方法?

C# 让两个变量中的一个随机拥有两个选项中的一个更简单的方法?,c#,random,C#,Random,我得到了两个变量,x和y。其中一个应具有从0到721的随机int值-此.Width。另一个值必须是0或721-此.Width。我已经设法创建了这个,但是对于这么小的事情来说,这么长的代码似乎很愚蠢。也许这是唯一(或最好)的解决方案,但我想确定的是,有没有一个更短的方法 这是我的密码: Random random = new Random(); int x, y; if (random.Next(2) == 1) { x = random.Next(721 - this.Width);

我得到了两个变量,
x
y
。其中一个应具有从
0
721的随机int值-此.Width
。另一个值必须是
0
721-此.Width
。我已经设法创建了这个,但是对于这么小的事情来说,这么长的代码似乎很愚蠢。也许这是唯一(或最好)的解决方案,但我想确定的是,有没有一个更短的方法

这是我的密码:

Random random = new Random();
int x, y;
if (random.Next(2) == 1)
{
    x = random.Next(721 - this.Width);
    if (random.Next(2) == 1)
    {
        y = 721 - this.Height;
    }
    else
    {
        y = 0;
    }
}
else
{
    y = random.Next(721 - this.Height);
    if (random.Next(2) == 1)
    {
        x = 721 - this.Width;
    }
    else
    {
        x = 0;
    }
}

如果希望占用更少的线路,则可以执行以下操作:

Random random = new Random();
int x, y;
switch (random.Next(2))
{
    case 1:
        x = random.Next(721 - Width);
        y = random.Next(2) == 1 ? 721 - Height : 0;
        break;
    default:
        y = random.Next(721 - Height);
        x = random.Next(2) == 1 ? 721 - Width : 0;
        break;
}

归功于Resharper。

如果您希望它占用更少的行数,则可以执行以下操作:

Random random = new Random();
int x, y;
switch (random.Next(2))
{
    case 1:
        x = random.Next(721 - Width);
        y = random.Next(2) == 1 ? 721 - Height : 0;
        break;
    default:
        y = random.Next(721 - Height);
        x = random.Next(2) == 1 ? 721 - Width : 0;
        break;
}

这要归功于Resharper。

还不错。我认为你能做的最好的事情就是把它移动到一个很好的助手实用程序中去隐藏它的复杂性。也许您可以将各种
随机分配。下一步(0,1)
结果分配给命名布尔:

public class PositionCalculator
{
    private Random random = new Random();

    public Point CalculatePosition(int width, int height)
    {
        int x, y;

        bool favourWidth = RandomBoolean();
        bool useZeroForOther = RandomBoolean();

        int favouredValue = random.Next(721 - (favourWidth ? width : height));
        int otherValue = useZeroForOther ? 0 : (721 - (favourWidth ? height : width));

        if (favourWidth)
        {
            x = favouredValue;
            y = otherValue;
        }   
        else
        {
            x = otherValue;
            y = favouredValue;
        }

        return new Point() { X = x, Y = y };
    }

    private bool RandomBoolean()
    {
        return random.Next(2) == 1;
    }
}
至少通过这种方式,但是如果您想使用内部实现,它对应用程序的其余部分实际上并不重要。我让它传入
宽度
高度
,只是为了避免要求它引用UI层


编辑:真的,即使这样,我仍然发现三元运算符很难遵循“逻辑”路径。请随意使用对您来说最有意义的ifs或方法结构来维护它,并且在几个月/几年后再次查看算法时仍能理解它。

还不错。我认为你能做的最好的事情就是把它移动到一个很好的助手实用程序中去隐藏它的复杂性。也许您可以将各种
随机分配。下一步(0,1)
结果分配给命名布尔:

public class PositionCalculator
{
    private Random random = new Random();

    public Point CalculatePosition(int width, int height)
    {
        int x, y;

        bool favourWidth = RandomBoolean();
        bool useZeroForOther = RandomBoolean();

        int favouredValue = random.Next(721 - (favourWidth ? width : height));
        int otherValue = useZeroForOther ? 0 : (721 - (favourWidth ? height : width));

        if (favourWidth)
        {
            x = favouredValue;
            y = otherValue;
        }   
        else
        {
            x = otherValue;
            y = favouredValue;
        }

        return new Point() { X = x, Y = y };
    }

    private bool RandomBoolean()
    {
        return random.Next(2) == 1;
    }
}
至少通过这种方式,但是如果您想使用内部实现,它对应用程序的其余部分实际上并不重要。我让它传入
宽度
高度
,只是为了避免要求它引用UI层


编辑:真的,即使这样,我仍然发现三元运算符很难遵循“逻辑”路径。请随意使用对您最有意义的ifs或方法结构来维护它,并在数月/数年后再次查看算法时仍能理解它。

您可以这样写:

Random random = new Random();
int a = random.Next(2) * (721 - this.Width);
int b = random.Next(721 - this.Width);
int c = random.Next(2) * (721 - this.Height);
int d = random.Next(721 - this.Height);
int x, y;

Boolean t = (random.Next(2) == 1);
x = (t) ? a : b;
y = (t) ? d : c;

请注意,如果您发现较长的版本更容易理解,则此代码并不比您的代码好。编写代码没有正确的方法,可理解性往往比简洁更重要。

您可以这样编写:

Random random = new Random();
int a = random.Next(2) * (721 - this.Width);
int b = random.Next(721 - this.Width);
int c = random.Next(2) * (721 - this.Height);
int d = random.Next(721 - this.Height);
int x, y;

Boolean t = (random.Next(2) == 1);
x = (t) ? a : b;
y = (t) ? d : c;
var max = 721 - this.Width;
var rand = new Random();
var r = rand.Next(max * 2);
var x = r % max;
var y = (r / max) * max;
if (rand.Next(2) == 1) {var t = x; x = y; y = t;}

请注意,如果您发现较长的版本更容易理解,则此代码并不比您的代码好。没有正确的方法来编写代码,可理解性往往比简洁更重要。

我同意,只要将它放在一个命名良好的方法中,您就可以轻松地进行单元测试并利用它,而不必担心它的长度;我认为有太多的逻辑门,要在不失去意义的情况下简化更多。然而,我想知道你的
y=721-这个.Height
x=721-此高度行是正确的。你打算让其中一个减法
宽度
吗?@ChrisSinclair很好,修正了它:P但我想我必须处理这个问题,代码看起来很糟糕,但谁在乎,它是有效的^^^^我同意,只要将它放在一个命名良好的方法中,你就可以轻松地进行单元测试并利用它,而不用担心它的长度;我认为有太多的逻辑门,要在不失去意义的情况下简化更多。然而,我想知道你的
y=721-这个.Height
x=721-此高度行是正确的。你打算让他们中的一个减去宽度吗?@ChrisSinclair很好,修正了:P但我想我必须处理这个问题,代码看起来很糟糕,但谁在乎呢,它是有效的^^
var max = 721 - this.Width;
var rand = new Random();
var r = rand.Next(max * 2);
var x = r % max;
var y = (r / max) * max;
if (rand.Next(2) == 1) {var t = x; x = y; y = t;}