Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to generates high-quality random numbers using RNGCryptoServiceProvider() in C#

3 Answers

0 votes
using System;
using System.Security.Cryptography;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                byte[] arr = new byte[4];

                for (int i = 0; i < 10; i++)
                {
                    rng.GetBytes(arr);

                    int n = BitConverter.ToInt32(arr, 0);
                    Console.WriteLine(n);
                }
            }
        }
    }
}


/*
run:
    
1382154183
1385868911
-841902702
-68443939
-1697870295
-824491233
-767157963
2003038576
-1294213352
-1576860415

*/

 



answered Feb 13, 2017 by avibootz
0 votes
using System;
using System.Security.Cryptography;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                byte[] arr = new byte[8];

                for (int i = 0; i < 10; i++)
                {
                    rng.GetBytes(arr);

                    long l = BitConverter.ToInt64(arr, 0);
                    Console.WriteLine(l);
                }
            }
        }
    }
}


/*
run:
    
1105285152613526239
-7526214951867634496
2229869605864289356
4740238405064005438
7225780467784143383
1794344645003941858
3498039857658745371
1476085524233489547
-1513230619995224599
7923792023615854548

*/

 



answered Feb 13, 2017 by avibootz
0 votes
using System;
using System.Security.Cryptography;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                byte[] arr = new byte[2];

                for (int i = 0; i < 10; i++)
                {
                    rng.GetBytes(arr);

                    int n = BitConverter.ToInt16(arr, 0);
                    Console.WriteLine(n);
                }
            }
        }
    }
}


/*
run:
    
-9119
7040
-6459
17857
5422
20904
21091
18887
25836
-7054

*/

 



answered Feb 13, 2017 by avibootz

Related questions

1 answer 158 views
1 answer 163 views
1 answer 124 views
2 answers 150 views
...