skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. In order to include a percent change for an outcome, you should first get a random percent.
    There are several variants to do so.

    This returns an int from 0 to 100 (101 exclusive). The problem with it is that you cannot calculate a decimal percent e.g. 99.5%.

    random.Next(0, 101);
    

    If that’s a problem for you and your goal is to calculate a decimal percent, you should use random.NextDouble(), which returns a random double from 0 to 1. The value can be both 0.47 and 0.99, where 0 is 0% and 1 is 100%.

    random.NextDouble();
    

    So the final solution for your task looks like this. We get a decimal percent and return a random number based on it.

    public int GetValue()
    {
        System.Random random = new();
    
        // get a double 0 - 1, where 0 is 0% and 1 is 100%
        double percent = random.NextDouble();
    
        // get a random value based on the percent
        return percent switch
        {
            <= .99 => random.Next(0, 401), // random from 0 to 400
            _ => random.Next(401, 501) // random from 400 to 500
        };
    }
    

    Make sure to divide the desired percent by 100. So if you need 99%, it’ll be 99 / 100 = .99. Random.Next(inclusive, exclusive)‘s 2nd value is always exclusive, so you should always add 1 to it.

    < .1 => random.Next(0, 101)     //  0% - <10%    =>  0 - 100
    <= .5 => random.Next(101, 501)  //  10% - 50%    =>  100 - 500
    _ => random.Next(501, 1001)     //  >50% - 100%  =>  0 - 101
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search