Tuesday, November 1, 2022

Generate Random Numbers in .NET6

 We can generate random number using the following code:

Console.WriteLine("Generate Randoms!");

var objRandom = new Random();

var myValue = objRandom.Next();


In the above example, you obtain an int number as result.

You can use the methods NextBytes and NextDouble to obtain the results to obtain random bytes or random doubles values.

In .NET 6 you can use the class RandomNumberGenerator present in the namespace System.Security.Cryptography.

  Console.WriteLine("Generate Randoms in .NET 6!");

   var objRandom = RandomNumberGenerator.Create();

   var objBytes = new byte[sizeof(int)]; // 4 bytes


    var myResult = BitConverter.ToInt32(objBytes, 0);


No comments:

Post a Comment

LIveCharts2: Charts for Windows and web

Charts for Windows and web including .Net MAUI  LiveCharts - LiveCharts2 (lvcharts.com)