Page 1 of 1

Not Random Enough

Posted: Thu Nov 06, 2014 4:55 am
by Xaos
Hey guys. I'm creating an economics model in C#, and it's going great, but I can't seem to get my population random enough and I'm not sure why. Included is the code, I'll go through and comment it up more tomorrow. Any help?

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EconomicSimulator
{
    class Citizen
    {
        public static int Population;
        public static List<long> citizenList = new List<long>();

        public static void Populate()
        {
            long income;
            Random rnd = new Random();
            int ageRand = rnd.Next(30, 41);
            Population = 40000000;
            for (int i = 0; i <= Population; i++)
            {
                int Age = rnd.Next(0, 100);

                if (Age < ageRand)
                {
                    Population--;
                }
            } 
            for (int i = 0; i <= Population; i++)
            {
                double incomeChance = (rnd.Next(0, 100000) + rnd.Next(0,1750000))/2;
                if (incomeChance <= 1)
                {
                    income = 10000000 * rnd.Next(10000, 70000);
                    citizenList.Add(income);
                    // Over $10,000,000
                }
                else if (incomeChance == 2)
                {
                    income = 1000*rnd.Next(5000, 10000);
                    citizenList.Add(income);
                    // $5,000,000 to $10,000,000
                }
                else if (incomeChance > 2 && incomeChance <= 6)
                {
                    income = 1000 * rnd.Next(1500, 2000);
                    citizenList.Add(income);
                    // $1,500,000 to $2,000,000
                }
                else if (incomeChance > 6 && incomeChance <= 1006)
                {
                    income = 1000 * rnd.Next(2000, 5000);
                    citizenList.Add(income);
                    // $2,000,000 to $5,000,000
                }
                else if (incomeChance > 1006 && incomeChance <= 2006)
                {
                    income = 1000 * rnd.Next(1000, 1500);
                    citizenList.Add(income);
                    // $1,000,000 to $1,500,000
                }
                else if (incomeChance > 2006 && incomeChance <= 6006)
                {
                    income = 100 * rnd.Next(5000, 10000);
                    citizenList.Add(income);
                    // $500,000 to $1,000,000
                }
                else if (incomeChance > 60006 && incomeChance <= 23006)
                {
                    citizenList.Add(1);
                    // No income
                }
                else if (incomeChance > 23006 && incomeChance <= 49006)
                {
                    income = 100*rnd.Next(2000, 5000);
                    citizenList.Add(income);
                    // $200,000 to $500,000
                }
                else if (incomeChance > 49006 && incomeChance <= 111006)
                {
                    income = 10 * rnd.Next(2500, 3000);
                    citizenList.Add(income);
                    // $25,000 to $30,000
                }
                else if (incomeChance > 111006 && incomeChance <= 181006)
                {
                    income = 10 * rnd.Next(2000, 2500);
                    citizenList.Add(income);
                    // $20,000 to $25,000
                }
                else if (incomeChance > 181006 && incomeChance <= 255006)
                {
                    income = rnd.Next(1, 5000);
                    citizenList.Add(income);
                    // $1 to $5,000
                }
                else if (incomeChance > 255006 && incomeChance <= 331006)
                {
                    income = 10 * rnd.Next(4000, 5000);
                    citizenList.Add(income);
                    // $40,000 to $50,000
                }
                else if (incomeChance > 331006 && incomeChance <= 413006)
                {
                    income = 10 * rnd.Next(7500, 10000);
                    citizenList.Add(income);
                    // $75,000 to $100,000
                }
                else if (incomeChance > 413006 && incomeChance <= 495006)
                {
                    income = 10 * rnd.Next(1500, 2000);
                    citizenList.Add(income);
                    // $15,000 to $20,000
                }
                else if (incomeChance > 495006 && incomeChance <= 580006)
                {
                    income = rnd.Next(5000, 10000);
                    citizenList.Add(income);
                    // $5,000 to $10,000
                }
                else if (incomeChance > 580006 && incomeChance <= 669006)
                {
                    income = 10 * rnd.Next(1000,1500);
                    citizenList.Add(income);
                    // $10,000 to $15,000
                }
                else if (incomeChance > 669006 && incomeChance <= 769006)
                {
                    income = 10 * rnd.Next(3000, 4000);
                    citizenList.Add(income);
                    // $30,000 to $40,000
                }
                else if (incomeChance > 769006 && incomeChance <= 871006)
                {
                    income = 100 * rnd.Next(1000, 2000);
                    citizenList.Add(income);
                    // $100,000 to $200,000
                }
                else if (incomeChance > 871006 && incomeChance <= 1001006)
                {
                    income = 10 * rnd.Next(5000, 7500);
                    citizenList.Add(income);
                    // $50,000 to $75,000
                }
            }
        }
    }
}

Re: Not Random Enough

Posted: Fri Nov 07, 2014 7:01 am
by Winawer
Your condition is wrong for no income (60006 should be 6006), maybe that's the problem?

What's the expected result and what are you getting?