1

Helo! My code is:

    class Program
        {
            static void Main(string[] args)
            {
                for (int x = 0; x < 15000; x++)
                {
                    int minValue = 1;
                    int maxValue = 1400;
                var rand = new Random();

                List<float> weights = new List<float>();
                List<float> numbers = new List<float>();
                for (int i = minValue; i <= maxValue; i++)
                {
                    weights.Add((int)Math.Pow(i, 0.4));
                    numbers.Add(i);
                }
                weights.Reverse();

                int weightSum = 0;
                foreach (int weight in weights)
                {
                    weightSum += weight;
                }

                List<int> possibleNumberList = new List<int>();

                float randomNumber = rand.Next(minValue, weightSum);

                float leastDifference = 2000000000f;
                int leastDifferenceNumberIndex = -1;
                for (int weightIndex = 0; weightIndex < weights.Count - 1; weightIndex++)
                {
                    if (Math.Abs(weights[weightIndex] - randomNumber) == leastDifference)
                    {
                        leastDifference = Math.Abs(weights[weightIndex] - randomNumber);
                        leastDifferenceNumberIndex = weightIndex;

                        possibleNumberList.Add(weightIndex);
                    }
                    else if (Math.Abs(weights[weightIndex] - randomNumber) < leastDifference)
                    {
                        leastDifference = Math.Abs(weights[weightIndex] - randomNumber);
                        leastDifferenceNumberIndex = weightIndex;
                        possibleNumberList = new List<int>();
                        possibleNumberList.Add(weightIndex);
                    }
                }

                var randFromListR = new Random();

                int listCunt = possibleNumberList.Count;
                int index = randFromListR.Next(0, listCunt);
                WWriteStringToNewLine(possibleNumberList[index].ToString());
            }
        }
    }

My goal is to have a list or array shown in the image peresentatin

Although my max value for me would be 1400.

With my code I can only achieve the following output: bad

If we zoom in, we can see that there are some higher numbers but generated only once. badZoom

If we set the code's max value to 10 the output is the following:

{3: 2837, 0: 2813, 4: 2781, 2: 2761, 1: 2759, 5: 273, 6: 264, 7: 262, 8: 250}

Max value 10

What could I change on this code to work correctly? What do you suggest? You can even give me a whole different code.

Marci
  • 57
  • 8
  • 2
    "Biased towards the lower end" this is very vague and can be achieved in a whole range of different ways (basic example would be to generate 2 random numbers and just return the lowest one). You then show a histogram but don't say if that's just an example of a distribution biased towards to lower end or if you want that exact distribution? (and would finding a formula fit be good enough or do you want to draw random numbers by sampling the provided histogram?) –  Nov 19 '20 at 08:59

1 Answers1

1

If you apply a function to your random result that will change your number repartition. I tried these : Exp(Random.Next(0,10000) / 100d) will generate a number with the repartition you apparently seek

Sqrt(Random.Next(0,10000)) will generate a number from 0 to 100 with a square root repartition.

You can take the integer part if needed

Warny
  • 27
  • 3