E-Learning

Learn C# – How To Generate a Random Int Number

c# random number generator

If you want to know how to generate a Random Int number using C#, then you have come to the right place.C# is one of the main .NET languages and is used to build Windows apps. We will use it to generate Random Int numbers.

To generate a Random Int Number using C#, you need to use the following code.

How To Generate a Random Int Number

As you can see from the above code, the Next() function is used to generate a random number. The only downside is that you need to input both a starting and ending value. However, it should always be kept in mind that the minValue(starting value) is inclusive, whereas maxValue(ending value) is exclusive.

Let’s take a look at the function.

Next(minValue, MaxValue)

The biggest mistake that learners make with the Next() function is to think that the random value is generated in between the min and max values. However, that’s not the case. So, if you want to randomly generate the month for your app (int rand_month in the example), you need to set minValue to 1 and maxValue to 13. You can read more about here.

System dependent generation

Another aspect that you need to understand is that the random value generation depends on the system clock. So, if you run two random generators at the same time, they will generate the same value rather than two different values. To overcome the problem, you need to store the first random value and then generate another random value.

Another caveat that you need know is that you won’t be able to generate 32 or 64-bit random bits. For generating 32 or 64-bit random bits, you need to use the NextInt32() or NextInt64() extension methods from Medallion Random.

If you have any questions regarding Random Int number generation in C#, then comment below and let us know.

You can check on our website videos about C#. Below are some examples:

  • Pokémon GO API’s (part 2) – C#

  • [C#] MySQL EntityFramework for .NET Core (part 2) – C#

You can also follow some of our broadcasters who program in C#, as below:

  Draxu

  xLukeF 

Another cool way to find out interesting things about C# is to access our project page!

Also Check Learn C# – How To Get a Consistent Byte Representation of Strings?

Avatar
About author

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.