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

ENJOYING THIS POST?

Get 5% discount on your monthly subscription purchase

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?

  • John

    I think no other explanation could have better than this. thanks

Read previous post:
react-flux-tutorial
Tutorial: 5 Things You Should Know About React Flux

Neither the latest anime title nor requiring 1.21 jigawatts of power, React Flux is really a powerful set of tools...

Close