How to make a random number generator C#

[By JayDev64x] [Index]

RNG's are an essential concept to learn if you plan to create games, here is a simple example of one created for usage in the console. You simply enter in 2 numbers for the program to pick in between from and then choose the amount that is generated at once.

using System; namespace Script { class Example { Random random; void Start() { // Defines a random class to be used to generate the random numbers. random = new Random(); // Takes the 2 numbers to be used for the random.Next (actual generation of numbers). Console.WriteLine("Choose 2 numbers to pick randomly in between."); int minNum = Convert.ToInt32(Console.ReadLine()); int maxNum = Convert.ToInt32(Console.ReadLine()); // Determines the amount of loops the while statement will go through. Console.WriteLine("How many numbers will be generated?"); int genNum = Convert.ToInt32(Console.ReadLine()); // The loop used to generate each random number. while (genNum > 0) { // Generation and displaying of the random number. double generated = random.Next(minNum, maxNum); Console.WriteLine("Generated: " + generated); // This is so the loop will end at one point. genNum -= 1; } Console.WriteLine("Completed generation, press enter to reset."); Console.ReadLine(); Console.Clear(); Start(); } // Takes the program out of a static function. static void Main() { Example example = new Example(); example.Start(); } } }

Created [September 3, 2021]