You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#控制台应用中设置随机结果的概率权重?

解决方案

先修正原代码的小问题

原代码中每次调用GenerateRandomNumber都新建Random实例,短时间内多次调用会因为系统时钟种子重复导致生成相同随机数,建议把Random声明为类的静态成员。

实现概率化随机数逻辑

要实现99%概率生成0-400,1%概率生成401-500,步骤如下:

  • 先生成一个0-99的随机整数(共100个数值,对应100%概率)
  • 如果这个数小于99(对应99%概率),生成0到400的随机数
  • 否则(剩下1%概率),生成401到500的随机数

修改后的完整代码

using System;

namespace TextBasedAdvanture
{
    internal class Program
    {
        // 把Random声明为静态成员,避免重复创建导致的重复随机数
        private static readonly Random _random = new Random();

        static void Main(string[] args)
        {
            int loot = 0;
            string option;
            string playing = "Y";

            do
            {
                Console.WriteLine("--------Adventure--------");
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("Type 'Fish', 'Hunt' or 'Beg'");
                option = Console.ReadLine();

                switch (option.ToLower())
                {
                    case "fish":
                        Console.WriteLine("You go fishing");
                        loot = GenerateWeightedRandomNumber();
                        Console.WriteLine($"You fished ${loot} worth of fish");
                        break;
                    case "hunt":
                        Console.WriteLine("You go hunting");
                        loot = GenerateWeightedRandomNumber();
                        Console.WriteLine($"You hunted ${loot} worth of animals");
                        break;
                    case "beg":
                        Console.WriteLine("You go begging");
                        loot = GenerateWeightedRandomNumber();
                        Console.WriteLine($"You begged ${loot} worth of change");
                        break;
                    default:
                        Console.WriteLine("Please pick a valid option");
                        continue; // 重新循环,不执行后续代码
                }

                Console.WriteLine("Play Again? (Y/N)");
                playing = Console.ReadLine().ToUpper();
            } while (playing == "Y");
            Console.WriteLine("Thanks for Playing");
        }

        public static int GenerateWeightedRandomNumber()
        {
            // 生成0-99的随机数,对应100个概率区间
            int probabilityRoll = _random.Next(0, 100);
            
            if (probabilityRoll < 99)
            {
                // 99%概率:生成0到400的随机数(Next左闭右开,所以上限是401)
                return _random.Next(0, 401);
            }
            else
            {
                // 1%概率:生成401到500的随机数(上限是501)
                return _random.Next(401, 501);
            }
        }
    }
}

关键改动说明

  • 静态_random实例:确保每次调用随机数方法都是同一个实例,避免重复种子问题
  • GenerateWeightedRandomNumber方法:
    • 先通过_random.Next(0,100)获取概率判定值,0-98对应99%概率,99对应1%概率
    • 用Next(0,401)生成0到400的整数(因为Next的第二个参数是不包含的上限)
    • 用Next(401,501)生成401到500的整数

内容的提问来源于stack exchange,提问作者Jason Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 04:55:02