The following demonstrates how to use C# language to download random data from a AlphaRNG device when used with AlphaRNG Entropy Server for Windows. The following code will only work when running AlphaRNG Entropy Server. Before using the following example, make sure your C# project is targeted for Windows 64 bit platform and the AlphaRNG device is plugged into one of USB ports available. The AlphaRNG-64.dll is included in the software kit.

A sample source code for retrieving primitive type random values from AlphaRNG device when using Entropy Server for Windows and provided AlphaRNG-64.dll


using System;
using System.Runtime.InteropServices;

namespace AlphaRngSampleApp2
{
    class Program
    {
        // Include the exact path to the AlphaRNG-64.dll (it uses additional dependencies from same location) available with the software kit.
        [DllImport("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll", EntryPoint = "getEntropy")]
        public static extern int getRandomDouble(ref double d, long byteCount);

        [DllImport("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll", EntryPoint = "getEntropy")]
        public static extern int getRandomInt(ref int i, long byteCount);

        [DllImport("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll", EntryPoint = "getEntropy")]
        public static extern int getRandomUInt64(ref UInt64 ui64, long byteCount);

        [DllImport("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll", EntryPoint = "getEntropy")]
        public static extern int getRandomByte(ref byte b, long byteCount);

        [DllImport("C:\\tools\\alrng-windows-binaries\\windows-x64-vs-2019\\AlphaRNG-64.dll", EntryPoint = "getEntropy")]
        public static extern int getRandomBytes(byte[] b, long byteCount);

        static void Main(string[] args)
        {
            double d = 0;
            int status = getRandomDouble(ref d, sizeof(double));
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve a random double, is Entropy Server running?");
            }
            else
            {
                Console.Out.WriteLine("Random double: " + d);
            }

            int n = 0;
            status = getRandomInt(ref n, sizeof(int));
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve a random int from AlphaRNG Entropy Server");
            }
            else
            {
                Console.Out.WriteLine("Random int: " + n);
            }

            UInt64 ui64 = 0;
            status = getRandomUInt64(ref ui64, sizeof(UInt64));
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve a random Uint64 from AlphaRNG Entropy Server");
            }
            else
            {
                Console.Out.WriteLine("Random Uint64: " + ui64);
            }

            byte b = 0;
            status = getRandomByte(ref b, sizeof(byte));
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve a random byte from AlphaRNG Entropy Server");
            }
            else
            {
                Console.Out.WriteLine("Random byte: " + b);
            }

            byte[] byteArray = new byte[10];
            status = getRandomBytes(byteArray, byteArray.Length);
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve a byte array from AlphaRNG Entropy Server");
            }
            else
            {
                for (int i = 0; i < byteArray.Length; i++)
                {
                    Console.Out.WriteLine("Random byte [" + i + "] : " + byteArray[i]);
                }

            }

            // For maximum performance, download multiple bytes with one call.
            // Can only retrieve up to 100000 bytes at a time.
            byte[] maxByteArray = new byte[100000];
            status = getRandomBytes(maxByteArray, maxByteArray.Length);
            if (status != 0)
            {
                Console.Out.WriteLine("Could not retrieve " + maxByteArray.Length + " bytes array from AlphaRNG Entropy Server");
            }
            else
            {
                Console.Out.WriteLine("Retrieved an array of " + maxByteArray.Length + " bytes successfully");
            }


        }
    }
}