C# random number generator.

In this blog, I will show you how to create a random number generator for a fictitious lottery that displays random values and why not use Random for anything slightly complex.

The code for this example is written in C# MVC and you can see the full working example by downloading the code from Github.

So let's get started, first create the page that will display the random numbers generated.

<div class="container">
    <div class="row">
        <div clas="col-12">
            

            @using (Html.BeginForm("GetLotteryNumbers", "RandomInt", FormMethod.Post))
            {
            <div>
                <p>
                    Line One:
                    <ol class="list-inline">
                        @foreach (var number in ViewBag.LineOne)
                        {
                            <li class="list-inline-item"><span class="badge badge-danger">@number</span></li>
                        }
                    </ol>

                <p>Line Two:</p>
                <ol class="list-inline">
                    @foreach (var number in ViewBag.LineTwo)
                    {
                    <li class="list-inline-item"><span class="badge badge-danger">@number</span></li>
                    }
                </ol>
                <p>Line Three:</p>
                <ol class="list-inline">
                    @foreach (var number in ViewBag.LineThree)
                    {
                    <li class="list-inline-item"><span class="badge badge-danger">@number</span></li>
                    }
                </ol>
                <p>Line Four:</p>
                <ol class="list-inline">
                    @foreach (var number in ViewBag.LineFour)
                    {
                    <li class="list-inline-item"><span class="badge badge-danger">@number</span></li>
                    }
                </ol>
                <input type="submit" value="Generate Numbers" />
            </div>
            }
        </div>
    </div>
</div>

Controller.

Now we need to write the code for the c# random number generator for the numbers generated, this code can be found below.

You can see in the code that I have some static classes and within the LoopLotteryNumbers method, I have a while(true) loop, then a for loop that creates random numbers. I also check for duplicates in the list and if there are any, then I re-run the for loop until the random numbers are unique.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Web.Mvc;

namespace Web.Controller.Controllers
{
    public class RandomIntController : System.Web.Mvc.Controller
    {
        [HttpGet]
        public ActionResult Home()
        {
            ViewBag.LineOne    = "";
            ViewBag.LineTwo    = "";
            ViewBag.LineThree  = "";
            ViewBag.LineFour   = "";

            return View();
        }

        [HttpPost]
        public ActionResult GetLotteryNumbers()
        {
            const int min       = 1;
            const int max       = 60;
            const int rndMax    = 61;

            ViewBag.LineOne         = NewRandom.LoopLotteryNumbers(5, min, rndMax, 1).OrderBy(x=>x);
            ViewBag.LineTwo         = NewRandom.LoopLotteryNumbers(5, min, rndMax, 2).OrderBy(x => x);
            ViewBag.LineThree       = NewRandom.LoopLotteryNumbers(5, min, max, 3).OrderBy(x => x);
            ViewBag.LineFour        = NewRandom.LoopLotteryNumbers(5, min, max, 3).OrderBy(x => x);


            return View("~/Views/RandomInt/Home.cshtml");
        }
    }

    public static class NewRandom
    {
        public static List LoopLotteryNumbers(int numberOfLoops, int min, int max, int type)
        {
            
            List lstNumbers = new List();
            lstNumbers.Clear();

            while (true)
            {
                for (int i = 0; i <= numberOfLoops; i++)
                {
                    switch (type)
                    {
                        case 1:
                            Thread.Sleep(200);//Comment this line to see what happens
                            lstNumbers.Add(GetRandomNumber(min, max));
                            break;
                        case 2:
                            lstNumbers.Add(GetRandomRangeNumber(min, max));
                            break;
                        case 3:
                            lstNumbers.Add(GetRandomNumberCrypto(min, max));
                            break;
                    }
                }
                int lstdups = lstNumbers.GroupBy(x => x).Sum(x=>x.Count() - 1);
                if (lstdups > 0)
                {
                    //If we have dupliacte numbers, clear the list and start again
                    lstNumbers.Clear();
                }
                else
                {
                    break;
                }
            }
            return lstNumbers;
        }

        private static int GetRandomNumber(int min, int max)
        {
            //Very bad for anything slightly complex
            Random rnd = new Random();
            int lotteryNumber = rnd.Next(min, max);

            return lotteryNumber;
        }

        private static readonly Random GetNewRandomNumber = new Random();

        private static int GetRandomRangeNumber(int min, int max)
        {
            lock (GetNewRandomNumber)
            {
                return GetNewRandomNumber.Next(min, max);
            }
        }

        private static int GetRandomNumberCrypto(int min, int max)
        {
            int result;
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                byte[] randomNumber = new byte[4];
                rng.GetBytes(randomNumber);
                int value = BitConverter.ToInt32(randomNumber, 0);
                result = value;

                var prop = (max - min + 0d) / int.MaxValue;
                result = Math.Abs((int) Math.Round(result * prop));
                result += min;
            }

            return result;
        }
    }
}

Summary.

In the LoopLotteryNumbers method, comment out Thread.Sleep(200) and see what effect this has on application.

C# random number generator with real world example

I hope you found this short blog helpful in creating a C# random number generator.