C # 범위의 하한쪽으로 편향된 범위에서 난수를 생성하는 방법은 무엇입니까? [복제]

Nov 19 2020

헬리콥터! 내 코드는 다음과 같습니다.

    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());
            }
        }
    }

내 목표는 이미지에 목록이나 배열을 표시하는 것입니다.

저의 최대 가치는 1400입니다.

내 코드로 다음 출력 만 얻을 수 있습니다.

확대하면 더 높은 숫자가 있지만 한 번만 생성되는 것을 볼 수 있습니다.

코드의 최대 값을 10으로 설정하면 출력은 다음과 같습니다.

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

올바르게 작동하려면이 코드에서 무엇을 변경할 수 있습니까? 당신은 무엇을 제안합니까? 나에게 완전히 다른 코드를 줄 수도 있습니다.

답변

1 Warny Nov 19 2020 at 09:16

무작위 결과에 함수를 적용하면 번호 재분할이 변경됩니다. 나는 이것을 시도했다 : Exp (Random.Next (0,10000) / 100d) 당신이 분명히 찾고있는 repartition으로 숫자를 생성합니다

Sqrt (Random.Next (0,10000))는 제곱근 재분할로 0에서 100까지의 숫자를 생성합니다.

필요한 경우 정수 부분을 사용할 수 있습니다.