How to generate random 5 digit number depend on us

2019-03-06 16:56发布

Hi guy i try to generate 50 number with 5 digit depend on user total summary. For example, User give 500000 and then i need to random number with 5 digit by 50 number equal 500000

i try this but it isn't 5 digit number

int balane = 500000;
            int nums = 50;
            int max = balane / nums;
            Random rand = new Random();
            int newNum = 0;
            int[] ar = new int[nums];
            for (int i = 0; i < nums - 1; i++)
            {
                newNum = rand.Next(0, max);
                ar[i] = newNum;
                balane -= newNum;
                max = balane / (nums - i - 1);

                ar[nums - 1] = balane;
            }

            int check = 0;
            foreach (int x in ar)
            {
                check += x;
            }

i tried already but value inside my array have negative value i want to get only positive value

enter image description here

Please help me, how to solve this and thank for advance.

2条回答
家丑人穷心不美
2楼-- · 2019-03-06 17:33

I once asked a similar question on codereview.stackexchange.com. I have modified my answer to produce a five digit sequence for you.

Furthermore, this code is fast enough to be used to create tens of thousands of codes in a single request. If you look at the initial question and answer (linked to below) you will find that it checks to see whether the code has been used or not prior to inserting it. Thus, the codes are unique.

void Main()
{
    Console.WriteLine(GenerateCode(CodeLength));
}

private const int CodeLength = 10;
// since Random does not make any guarantees of thread-safety, use different Random instances per thread
private static readonly ThreadLocal<Random> _random = new ThreadLocal<Random>(() => new Random());

// Define other methods and classes here
private static string GenerateCode(int numberOfCharsToGenerate)
{
    char[] chars = "0123456789".ToCharArray();

    var sb = new StringBuilder();
    for (int i = 0; i < numberOfCharsToGenerate; i++)
    {
        int num = _random.Value.Next(0, chars.Length);
        sb.Append(chars[num]);
    }
    return sb.ToString();
}

Original question and answer: https://codereview.stackexchange.com/questions/142049/creating-a-random-code-and-saving-if-it-does-not-exist/142056#142056

查看更多
做个烂人
3楼-- · 2019-03-06 17:51

Perhaps try this:

var rnd = new Random();

var numbers = Enumerable.Range(0, 50).Select(x => rnd.Next(500_000)).OrderBy(x => x).ToArray();

numbers = numbers.Skip(1).Zip(numbers, (x1, x0) => x1 - x0).ToArray();

numbers = numbers.Append(500_000 - numbers.Sum()).ToArray();

Console.WriteLine(numbers.Count());
Console.WriteLine(numbers.Sum());

This outputs:

50
500000

This works by generating 50 random numbers between 0 and 499,999 inclusively. It then sorts them ascendingly and then gets the difference between each successive pair. This by definition produces a set of 49 values that almost adds up to 500,000. It's then just a matter of adding the one missing number by doing 500_000 - numbers.Sum().

查看更多
登录 后发表回答