I am struggling with a randomize algorithm which I'm unable to solve.
Below is the randomize criteria.
- User type a random integer number, i.e 28
- User type a factor number, i.e 1.2
- If user key in 28 on point #1 and 1.2 for point #2, then there should be a total of 28 randomize number generated and the sums of the 28 randomize number must equals to 29.2
- For each randomize number must be the value between 0.01 to 9.99, maximum two decimals.
I have done my code and it meet criteria 1-3, but I can't seems to meet criteria #4. If a lot of high randomize number generated upfront, the ending iteration would not be enough to generate at least 0.01. It was always 0.00. Anything i missed out?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RandomizeAlgo
{
    public static class Extend
    {
        public static double RandomNumberBetween(this Random random, double minValue, double maxValue)
        {
            var next = random.NextDouble();
            return minValue + (next * (maxValue - minValue));
        }
        public static double ToFloor(this double value)
        {
            return Math.Floor(value * 100) / 100;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var rnd = new Random();
            var totalPeople = 28;
            var factor = 1.2;
            var shouldHaveTotalNumber = totalPeople + factor;
            var defaultMin = 0.01;
            var defaultMax = 9.99;
            while (true)
            {
                var iteration = 0;
                var balance = shouldHaveTotalNumber;
                var listOfRandomizedNumber = new List<double>();
                for (var i = 1; i <= totalPeople; i++)
                {
                    var randomizeResult = 0.00;
                    if (i == totalPeople)
                    {
                        randomizeResult = balance;
                    }
                    else if (balance >= defaultMax)
                    {
                        randomizeResult = rnd.RandomNumberBetween(defaultMin, defaultMax);
                        randomizeResult = randomizeResult.ToFloor();
                    }
                    else
                    {
                        randomizeResult = rnd.RandomNumberBetween(defaultMin, balance);
                        randomizeResult = randomizeResult.ToFloor();
                    }
                    listOfRandomizedNumber.Add(randomizeResult);
                    Console.WriteLine(string.Format("{0:0.00}", randomizeResult));
                    balance = balance - randomizeResult;
                }
                iteration++;
                //Assertion
                var totalSumNumberGenerated = listOfRandomizedNumber.Sum().ToString("0.00");
                if (totalSumNumberGenerated != shouldHaveTotalNumber.ToString("0.00"))
                {
                    throw new InvalidOperationException("Total #"+ iteration + " iteration is: " + totalSumNumberGenerated + " . Invalid Randomize Number. Sum does not match");
                }
                else
                {
                    Console.WriteLine("Iteration #"+ iteration + " successfully generated");
                }
            }
        }
    }
}
 
     
    