【问题标题】:Generate a Random Number That Evenly Divides into Another in C#在 C# 中生成一个等分除以另一个的随机数
【发布时间】:2016-02-23 07:44:48
【问题描述】:

我正在做一个测验,测试用户的基本算术技能。我遇到的问题是我不想生成允许实数的除法问题。我希望所有答案都有整数答案。

如何在 p 和 q 之间随机生成一个可以整除 n 的数?

【问题讨论】:

  • 反过来做:生成两个数字,然后将它们的乘积用作所需的数字。
  • 没想到。以为我必须建立一个查找表或其他东西。我如何解释分子比其他生成的操作数大得多的事实?
  • 好吧,如果你需要一个介于 p 和 q 之间的数字,你可以随机生成第一个数字,然后为第二个生成范围 [p / first number; q / 第一个数字]
  • @CanadaIT 这里的答案是否与您想要的相符?

标签: c# .net random


【解决方案1】:

思路是生成两个整数a and n,然后返回ac = a * n。回答者应该猜出n 是什么并小心被零除

这样就可以了:

public KeyValuePair<int, int> GenerateIntDivisibleNoPair(int p, int q) {
    if (p <= 0 || q <= 0 || q <= p)
        throw Exception(); //for simplification of idea
    Random rand = new Random();
    int a = rand.Next(p, q + 1); //cannot be zero! note: maxValue put as q + 1 to include q
    int n = rand.Next(p, q + 1); //cannot be zero! note: maxValue put as q + 1 to include q
    return new KeyValuePair<int, int>(a, a * n);
}

你可以这样使用它:

KeyValuePair<int, int> val = GenerateIntDivisibleNoPair(1, 101);
Console.WriteLine("What is " + val.Value.ToString() + " divide by " + val.Key.ToString() + "?");

【讨论】:

  • 你也可以用 rand.Next(1,100) 代替生成的数字加一
  • @Viru 啊,是的。当然。我希望 100 具有包容性。这就是我放那个的原因。但你是对的。我们实际上可以把rand.Next(1, 101); :)
  • @Knickerless-Noggins (1) “这个解决方案忽略了生成的数字需要在 p 和 q 范围内。除此之外,它断言 p 和 n 都限制在 1 和100,这不是问题所要求的“你是对的,也许我应该更新我的答案。但是这个(2)“我猜是伊恩把我的正确和详细的解决方案减了一个”你错了,我什么都没做。
  • @Knickerless-Noggins 以证明我现在会投票支持你回答一段时间,然后再次取消投票...
  • 此解决方案忽略了生成的数字需要在 p 和 q 范围内。最重要的是,它断言 p 和 n 都限制在 1 到 100 之间,这不是问题所要求的。 ;)
【解决方案2】:

我会在某个地方声明一个具有全局访问权限的 Random:

public static Random Rnd { get; set; }

然后,当您想要一个数字除以另一个数字时,您会不断生成一个数字,直到您得到一个除以除数的数字:

if(Rnd == null)
{
    Rnd = new Random();

}

int Min = p; //Can be any number

int Max = q; //Can be any number

if(Min > Max) //Assert that Min is lower than Max
{
    int Temp = Max;
    Max = Min;
    Min = Temp;

}

int Divisor = n; //Can be any number

int NextRandom = Rnd.Next(Min, Max + 1); //Add 1 to Max, because Next always returns one less than the value of Max.

while(NextRandom % Divisor != 0)
{
    NextRandom = Rnd.Next(Min, Max + 1); //Add 1 to Max, because Next always returns one less than the value of Max.

}

检查使用模数函数%。此函数为您提供整数除法的余数。

这意味着如果 NextRandom % Divisor 为 0,则 Divisor 均分到 NextRandom。

这可以变成这样的方法:

public static int GetRandomMultiple(int divisor, int min, int max)
{
    if (Rnd == null)
    {
         Rnd = new Random();

    }

    if(min > max) //Assert that min is lower than max
    {
        int Temp = max;
        max = min;
        min = Temp;

    }

    int NextRandom = Rnd.Next(min, max + 1); //Add 1 to Max, because Next always returns one less than the value of Max.

    while (NextRandom % divisor != 0)
    {
        NextRandom = Rnd.Next(min, max + 1); //Add 1 to Max, because Next always returns one less than the value of Max.

    }

    return NextRandom;

}

然后你可以用你提到的变量来调用它:

int Number = GetRandomMultiple(n, p, q);

注意:由于“下一步”方法,我将 Max 的值加一。我认为这是.Net中的一个错误。 Max 的值永远不会返回,只有 Min..Max - 1。加一可以弥补这一点。

【讨论】:

  • 不,这不是错误 - 这是独家上限,专注于独家msdn.microsoft.com/en-gb/library/2dx6wyd4%28v=vs.110%29.aspx
  • 对不起,fubo,但我仍然认为它是一个错误,因为对范围的共同理解是值都是包含的。至少两者都应该是包容性的或排他性的。将两者混合只会混淆事物,尤其是作为 .Net 等高级 API 的默认操作。
  • 但这是一个记录在案的行为。我认为原因是实现referencesource.microsoft.com/mscorlib/system/random.cs.html
  • 是的,该行为已记录在案,但它应该以这种方式工作吗?我想不出一个单一的理由来支持为什么它会被设计成这样工作,我已经解释了为什么我认为范围应该是包容性的。
猜你喜欢
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
相关资源
最近更新 更多