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
You can generate a number two times.
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 andmax
is exclusive.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%
.If that’s a problem for you and your goal is to calculate a decimal percent, you should use
random.NextDouble()
, which returns a randomdouble
from0
to1
. The value can be both0.47
and0.99
, where0
is0%
and1
is100%
.So the final solution for your task looks like this. We get a decimal percent and return a random number based on it.
Make sure to divide the desired percent by
100
. So if you need99%
, it’ll be99 / 100 = .99
.Random.Next(inclusive, exclusive)
‘s 2nd value is always exclusive, so you should always add1
to it.