Retrace will reach End of Life on March 31, 2027. Click here to learn more.

How to Generate C# Random Numbers, Pseudo vs Secure Random Numbers

By: mwatson
  |  April 16, 2017

Random numbers are used in a wide variety of software applications. Depending on what you are using random numbers for, you will need to decide which type to use. For a music jukebox, the accuracy is not very critical. For something like a lottery or slot machine, the random number generator must be extremely accurate.  Read on to learn more about C# random numbers.

There are two types of random number generators in C#:

  • Pseudo-random numbers (System.Random)
  • Secure random numbers (System.Security.Cryptography.RNGCryptoServiceProvider)

Pseudo vs Secure Random Numbers

The key difference is the chance that the seed value used to do the randomization may not be changing quickly and randomly enough. For example, System.Random relies on the computer system clock. If multiple Random() objects were created at the exact same time, they could create the same sequence of random numbers.

Secure random numbers are called “secure” because of potential security vulnerabilities in weak random number generators. If a hacker could figure out a pattern to your random crypto keys, they may be able to increase their chances of hacking in.

MORE: “True” vs. pseudo-random numbers (Wikipedia)

How to use C# System.Random Number Generator

System.Random works great for basic use cases of creating random numbers.

For example, we use it at Stackify to randomly schedule some background jobs so they don’t all run exactly at the same second and minute every hour. We want to randomly distribute the jobs over the course of an hour. For this type of use case, System.Random works fine for us.

Examples of using System.Random to generate C# random numbers:

Random random = new System.Random();
int value = random.Next(0, 100); //returns integer of 0-100
double value2 = random.NextDouble(); //returns floating point 0.0-1.0

var byteArray = new byte[256];
random.NextBytes(byteArray); //fill with random bytes

 

How to Create Secure C# Random Numbers With RNGCryptoServiceProvider

Secure random numbers are important if you are creating a very high volume of numbers or need to ensure proper uniqueness over a long period of time. If you were generating random numbers for a game like Roulette, it would be important that the random numbers were properly distributed over all of the potential outcomes (0-36) properly.

RNGCryptoServiceProvider provides a random set of bytes. You must then convert those bytes to the data type that you need.

RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
var byteArray = new byte[4];
provider.GetBytes(byteArray);

//convert 4 bytes to an integer
var randomInteger = BitConverter.ToUInt32(byteArray, 0);

var byteArray2 = new byte[8];
provider.GetBytes(byteArray2);

//convert 8 bytes to a double
var randomDouble = BitConverter.ToDouble(byteArray2, 0);

 

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d toto toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot pakde4d link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot rp888 www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot slot thailand patentoto situs judi bola situs toto roma77 slot jpmania toto228
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d toto toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot pakde4d link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot rp888 www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto coloktoto coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor slot gacor slot gacor slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot slot thailand patentoto situs judi bola situs toto roma77 slot jpmania toto228