Search code examples
c#random

How do I set chances for an outcome in c#?


So for a console app in Visual Studio, I want to make it so the user does an action and the action will give a random output of 0-500 indicating money. Currently, in my code 0-500 has an equal chance of generating as I only used the random function, I'm just wondering how I can set different chances to amounts. For example, how do i make it so there's a 99% chance the random function will give a result of 0-400 and only a 1% chance it goes from 401-500.

This is my current code:

using System;
using System.Runtime.Intrinsics.Arm;

namespace TextBasedAdvanture
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int min = 0;
            int max = 500;
            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 = GenerateRandomNumber(min, max);
                        Console.WriteLine($"You fished ${loot} worth of fish");
                        playing = "N";
                        break;
                    case "hunt":
                        Console.WriteLine("You go hunting");
                        loot = GenerateRandomNumber(min, max);
                        Console.WriteLine($"You hunted ${loot} worth of animals");
                        playing = "N";
                        break;
                    case "beg":
                        Console.WriteLine("You go begging");
                        loot = GenerateRandomNumber(min, max);
                        Console.WriteLine($"You begged ${loot} worth of change");
                        playing = "N";
                        break;
                    default:
                        Console.WriteLine("Please pick a valid option");
                        continue; //re-loops previous code without executing further
                }

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

        public static int GenerateRandomNumber(int min, int max)
        {
            Random random = new Random();
            for (int i = 0; i < 1; i++)
            {
                random.Next(min, max);
            }
            return random.Next(min, max);
        }
    }
}

I researched some forums for the solution or maybe if it's a function in C#. I have a feeling it's just some logical thinking that I'm missing, any help appreciated, I'm still fairly new to programming.


Solution

  • You can generate a number two times.

    Random random = new Random();
    int n1 = random.Next(1, 101);  // 1 <= n1 <= 100
    int n2;
    if (n1 <= 99) {  // 1 <= n1 <= 99
        n2 = random.Next(0, 401);  // 0 <= n2 <= 400
    } else {  // n1 == 100
        n2 = random.Next(401, 501);  // 401 <= n2 <= 500
    }
    

    Anyway, I think it would be better to set 0 <= n2 <= 399 for the first case and 400 <= n2 <= 499 for the second case, because 0 <= n2 <= 400 means n2 can have one among 401 integers.

    Note: In using random.Next(min, max), min is inclusive and max is exclusive.