简体   繁体   中英

Generate 4 random number whose sum is 100 and one is more than 50

I need to create 4 int random numbers whose sum is 100. And one of them is more than 50 and bigger than the others. I have this:

int a=0, b=0,c=0,d=0;
int cem=100;
while (a+b+c+d=cem){
Random perc = new Random();
a = perc.Next(50, 100);
b = perc.Next(0, 50);
c = perc.Next(0, 50);
d = perc.Next(0, 50);
}

in the compiler i get 2 errors:

The left-hand side of an assignment must be a variable, property of indexer Cannot implicitly convert type 'int' to 'bool'

Replace

while (a+b+c+d=cem){

with

while (a+b+c+d!=cem){

You're using assignment ( = ) instead of comparison ( == / != ).

In addition to the other answers regarding the compiler error message, you should also move the line

Random perc = new Random();

to the outside of the while loop. You don't need more than a single random number generator, and recreating it in a quick loop may produce identical results due to the time seed.

如果您考虑一下,四个随机数之和为100意味着它们中只有三个是随机数,而第四个是100减去其他三个数...因此,与其做循环,不如先生成一个数字,然后再生成一个带有其余数的数字间隔,然后是第三个。

why using a loop? good luck with getting what you want :-)

(so much cpu wasted)

here is how i would start doing it;

class Program
{
    static void Main(string[] args)
    {
        int a = 0, b = 0, c = 0, d = 0;
        int cem = 100;
        Random perc = new Random();

        a = perc.Next(50, cem);
        cem -= a;

        b = perc.Next(0, cem);
        cem -= b;

        c = perc.Next(0, cem);
        cem -= c;

        d = cem;

        Console.WriteLine("{0} + {1} + {2} + {3} = {4}",a,b,c,d,a+b+c+d);

        Console.ReadKey(false);
    }
}
The left-hand side of an assignment must be a variable
Cannot implicitly convert type 'int' to 'bool'

The while wants ==, assuming C# is like C. == is equality test, = is assignment.

(It should be obvious why this causes the first error message. You may need to think about why it explains the second, but since doing so is a good exercise I'm not going to explain.)

How about something like this, will less number of loops ?

int a = 0, b = 0, c = 0, d = 0;
int cem = 100;
Random perc = new Random();
a = perc.Next(50, cem);
b = perc.Next(0, cem - a);
c = perc.Next(0, cem - a - b);
d = cem - a - b - c;
class Program {
    void Main() {
        var random = new Random();

        // note it says one of them is more than 50
        // so the min value should be 51 not 50
        var a = random.Next(51, 100);

        // the rest of the number will be less than `a` 
        // because `a` is more than 50 so the max `remaining` 
        // will be is 49 (100 - 51)
        var remaining = 100 - a; 

        var b = random.Next(0, remaining);
        remaining -= b;

        var c = random.Next(0, remaining);
        remaining -= c;

        var d = remaining;

        Console.WriteLine("a: " + a);
        Console.WriteLine("b: " + b);
        Console.WriteLine("c: " + c);
        Console.WriteLine("d: " + d);
        Console.WriteLine("total: " + (a + b + c + d));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM