{"id":409,"date":"2021-04-16T21:49:14","date_gmt":"2021-04-16T21:49:14","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=409"},"modified":"2021-07-10T20:05:21","modified_gmt":"2021-07-10T20:05:21","slug":"randombigint-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/04\/16\/randombigint-cs\/","title":{"rendered":"RandomBigInt.cs"},"content":{"rendered":"\n<p>BigInteger Random Number Generator<\/p>\n\n\n\n<p>Updated: July-10,2021<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Numerics;\nusing System.Security.Cryptography;\npublic struct RandomBigInt\n{\n    private readonly RNGCryptoServiceProvider _crng;\n    private          int                      _maxByteWidth;\n    private          int                      _bitWidth;\n    public RandomBigInt(int bitWidth)\n    {\n        MaxValue          = BigIntegerHelper.GetMaxValueBitWidth(bitWidth);\n        MaxValue.BitWidth = bitWidth;\n        _maxByteWidth     = bitWidth >> 3;\n        OddsOnly          = false;\n        Unsigned          = false;\n        _crng             = new RNGCryptoServiceProvider();\n        _bitWidth         = bitWidth;\n    }\n    public bool OddsOnly;\n    public bool Unsigned;\n    public int BitWidth\n    {\n        get => _bitWidth;\n        set\n        {\n            _bitWidth         = value;\n            MaxValue          = BigIntegerHelper.GetMaxValueBitWidth(_bitWidth);\n            MaxValue.BitWidth = _bitWidth;\n            _maxByteWidth     = _bitWidth >> 3;\n        }\n    }\n    public BigDecimal MaxValue;\n    public bool NextBool()\n    {\n        return Sample() &lt; .5;\n    }\n    public BigInteger Next()\n    {\n        return Internal();\n    }\n    public BigInteger Next(BigInteger minValue, BigInteger maxValue)\n    {\n        if (minValue > maxValue)\n            throw new ArgumentException(\"maxValue must be greater than or equal to minValue\");\n        return ((BigInteger)(Sample() * (maxValue - minValue)) + minValue) &amp; (BigInteger)MaxValue;\n    }\n    public BigInteger Next(BigInteger maxValue)\n    {\n        if (maxValue &lt; 0)\n            throw new ArgumentException(\"maxValue must be greater than zero.\");\n        return (BigInteger)(Sample() * maxValue) &amp; (BigInteger)MaxValue;\n    }\n    public unsafe double NextDouble()\n    {\n        var buf = new byte[8];\n        GetBytes(buf);\n        fixed (byte* ptr = buf)\n        {\n            return *(ulong*)ptr * (1.0 \/ ulong.MaxValue) * ulong.MaxValue;\n        }\n    }\n    public BigRational NextBigRational()\n    {\n        return new BigRational(Internal(), Internal());\n    }\n    public decimal NextDecimal()\n    {\n        return new decimal((int)Next(int.MaxValue), (int)Next(int.MaxValue), (int)Next(int.MaxValue), NextBool(), (byte)Next(255));\n    }\n    public BigDecimal NextBigDecimal()\n    {\n        return Sample();\n    }\n    public byte[] GetNextByteArray(int size)\n    {\n        var ba = new byte[size];\n        _crng.GetBytes(ba);\n        return ba;\n    }\n    public char[] GetNextCharArray(int size)\n    {\n        var xbc = new byte[1];\n        var ca  = new char[size];\n        var ptr = 0;\n        do\n        {\n            _crng.GetBytes(xbc);\n            var c = xbc[0];\n            if (c >= 0x20 &amp;&amp; c &lt;= 0x7F)\n                ca[ptr++] = (char)c;\n        } while (ptr &lt; size);\n        return ca;\n    }\n    public char NextChar()\n    {\n        var xbc = new byte[1];\n        while (true)\n        {\n            _crng.GetBytes(xbc);\n            var c = xbc[0];\n            if (c >= 0x20 &amp;&amp; c &lt;= 0x7F)\n                return (char)c;\n        }\n    }\n    public bool[] GetNextBoolArrayLimit(int size)\n    {\n        var        ba = new bool[size];\n        const uint ll = uint.MaxValue >> 1;\n        for (var i = 0; i &lt; size; ++i)\n            ba[i] = Next(0, uint.MaxValue) > ll;\n        return ba;\n    }\n    public byte[] GetNextByteArrayLimit(int size, ulong minValue, ulong maxValue)\n    {\n        var ba = new byte[size];\n        for (var i = 0; i &lt; size; ++i)\n            ba[i] = (byte)Next(minValue, maxValue);\n        return ba;\n    }\n    public string GetRandomString(int Len)\n    {\n        return new string(GetNextCharArray(Len));\n    }\n    public string GetRandomString(int minLen, int maxLen)\n    {\n        return minLen == maxLen ? new string(GetNextCharArray(minLen)) : new string(GetNextCharArray((int)Next((uint)minLen, (uint)maxLen)));\n    }\n    private BigDecimal Sample()\n    {\n        var i = Internal();\n        var s = i * (BigDecimal.One \/ MaxValue);\n        if (s.IsZero)\n            throw new Exception($\"Sample is zero. Check BigDecimal BitWidth {MaxValue.BitWidth} Equals BitWidth {_bitWidth}\");\n        return s;\n    }\n    public void GetBytes(byte[] data)\n    {\n        if (data == null)\n            throw new ArgumentException(\"The buffer cannot be null.\");\n        _crng.GetBytes(data);\n    }\n    private BigInteger Internal()\n    {\n        if (Unsigned)\n        {\n            var buffer = new byte[_maxByteWidth + 1];\n            _crng.GetBytes(buffer);\n            buffer[_maxByteWidth] = 0;\n            var n = new BigInteger(buffer);\n            return !OddsOnly ? n : n | 1;\n        }\n        else\n        {\n            var buffer = new byte[_maxByteWidth];\n            _crng.GetBytes(buffer);\n            var n = new BigInteger(buffer);\n            return !OddsOnly ? n : n | 1;\n        }\n    }\n    public void NextBytes(byte[] buffer)\n    {\n        if (buffer == null)\n            throw new ArgumentNullException(\"The buffer cannot be null.\");\n        for (var index = 0; index &lt; buffer.Length; ++index)\n            buffer[index] = (byte)(Sample() * byte.MaxValue + 1);\n    }\n    public void GetNonZeroBytes(byte[] buffer)\n    {\n        if (buffer == null)\n            throw new ArgumentNullException(\"The buffer cannot be null.\");\n        var index = 0;\n        do\n        {\n            var v = (byte)(Sample() * byte.MaxValue + 1);\n            if (v > 0)\n            {\n                buffer[index] = v;\n                index++;\n            }\n        } while (index &lt; buffer.Length);\n    }\n    public string[] GetUniqueStringArray(int numberItems, int minLen, int maxLen)\n    {\n        var sl = new MSet15&lt;string>();\n        do\n        {\n            sl.Add(GetRandomString(minLen, maxLen));\n        } while (sl.Count &lt; numberItems);\n        return sl.ToArray();\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>BigInteger Random Number Generator Updated: July-10,2021<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[14,102,38,37,51],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/409"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}