简体   繁体   English

根据给定的总和生成30个随机数

[英]Generate 30 Random Numbers from a given sum

I am working on a report in C# in which I am given a total number of prints a department printed and I have to distribute them according to the month date that on 1st March 10 papers are printed out and so on to 31st March. 我正在使用C#编写一份报告,该报告中提供了部门打印的总数,我必须根据3月1日打印出纸张的月份(截至3月31日)进行分发。

I have a form which takes the total print outs count. 我有一张表格,它记录了总的打印输出数。 I have a Month Selector. 我有一个月选择器。

From Month I get the total days which is total numbers to be generated eg: 30 or 31 or 28 从Month中,我得到的总天数是要生成的总数,例如:30或31或28

Scenario : 场景:

In the month of March 2000 Prints outs Total Sum of Month : 2000 Numbers to be generated : 31 2000年3月打印输出月份总和:2000产生的数字:31

this is my code 这是我的代码

        int sum = 2345;
        int nums = 23;
        Random rand = new Random();
        int newNum = 0;
        int[] ar = new int[23];
        for (int i = 0; i < nums; i++)
        {
            newNum = rand.Next(0, sum);
            ar[i] = newNum;
            sum = sum - newNum;
        }
        for (int i = 0; i < 23 ; i++)
        {
            Console.WriteLine(ar[i]);

        }
        Console.ReadLine();

what happens is in the ending numbers it goes to zero . 发生的事情是在结尾数字变为零。 I want Normally distributed like on one index it stores the maximum value at first and in the end it decreases. 我希望正态分布像在一个索引上一样,它首先存储最大值,最后减小。

We have a thrid party Ricoh Print/PhotoCopier Machine installed and third party bills us with certain amount which they have calculate that our department has printed 3000 printouts so we have to distribute them in the days randomly, print out the report and get payment invoice from our department head. 我们安装了第三方第三方理光打印/影印机,第三方向我们开具了一定数量的账单,他们计算出我们部门已经打印了3000份打印输出,因此我们必须在几天内随机分发它们,打印出报告并从中获取付款发票我们部门的负责人

The department people are doing it on excel I offered them to give them a solution. 部门人员是在excel上完成的,我为他们提供了解决方案。 Windows form application is built and I just have to put this logic thats all.. Thank you for your feedbacks Windows窗体应用程序已构建,我仅需输入所有这些逻辑即可。.感谢您的反馈

You can do this easily with partitions. 您可以使用分区轻松地做到这一点。 For a 4 day month that produced 10 things: generate 3 random numbers between 0 and 10 (inclusive). 对于一个产生10件东西的4天月份:生成3个介于0和10(含)之间的随机数。 Sort them and append 10 to the list of numbers. 对它们进行排序,然后将10附加到数字列表中。 So we have perhaps: 因此,我们也许有:

3 6 6 10

Which partitions our prints: 哪个分区我们的印刷品:

p p p | p p p | | p p p p

If you want to have 23 random numbers with sum of 2345, you can use this code: 如果您想要23个随机数之和为2345,则可以使用以下代码:

        int sum = 2345;
        int nums = 23;
        int max = sum / nums;
        Random rand = new Random();
        int newNum = 0;
        int[] ar = new int[23];
        for (int i = 0; i < nums-1; i++) {
            newNum = rand.Next(max);
            ar[i] = newNum;
            sum-= newNum;
            max = sum / (nums-i-1);
        }
        ar[nums - 1] = sum;

It will give you: 它会给你:

在此处输入图片说明

Here is my idea for generating 30 random numbers with a specific sum: 这是我产生30个具有特定总和的随机数的想法:

int sum = 3000;
int size = 30; // assumes that (sum % size == 0)
int[] result = new int[size];
Random rand = new Random();
int x = sum / size;

for (int i = 0; i < size; i++)
{
    result[i] = x;
}

for (int i = 0; i < x; i++)
{
    var a = rand.Next(size - 1); // not sure if parameter is inclusive?
    var b = rand.Next(size - 1); // should return number between 0 and size-1 inclusively

    result[a]++;
    result[b]--;
}

int testSum = result.Sum(); // will equal "sum" (3000)

Lee Daniel Crocker linked to this , though, which I think is a better solution. 不过,李·丹尼尔·克罗克(Lee Daniel Crocker)与此相关 ,我认为这是一个更好的解决方案。 Very neat and intuitive. 非常简洁直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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