c# - 使用 Blum Blum Shub 算法的伪随机数生成器

标签 c# random numbers generator

我们需要在伪随机数生成器中实现 Blum Blum Shub 算法。我尝试在 c# 中搜索实现以得到一个想法,但没有成功。我们需要实现的一些方法不够清楚(或者我可能没有完全理解他们的要求)。

任何人都可以通过代码或类似方式提供一些帮助吗?我很难从文本中掌握概念。任何帮助都会被大大接受!

首先,我尝试遵循问题的逻辑。由于进展甚微,我开始在线搜索更好的解释,并可能找到实现以更好地理解。最后,我尝试用我认为有意义的内容填充一些请求的方法。

static long seed = 6367859;
static long p = 3263849;
static long q = 1302498943;
static long m = p*q;

// Generates a random bit i.e. 0 or 1 using the Blum Blum Shub Algorithm and the Least Significant Bit
private byte generateRandomBit(){ }

// Method to generate a single positive 32 bit random number using the Blum Blum Shub Algorithm.
// The generateRandomBit() method is used to generate the random bits that make up the random number
// Not complete!!
public int GenerateNextRandomNumber()
{
    int nextRandomNumber = (int)((p * seed + q) % m);

    seed = nextRandomNumber;

    return nextRandomNumber;
}

// Generates a random number between min and max.
// The GenerateNextRandomNumber() method must be used to generate the initial random number which must then be manipulated (if necessary) to be between min and max
public int GenerateNextRandomNumber(int min, int max){ }

// Uses the GenerateNextRandomNumber Method to generate a sequence of Random Numbers between the minimum and the maximum value using the Blum Blum Shub Algorithm
public int[] GenerateRadmonSequence(int n, int min, int max)
{
    int[] sequence = new int[n];

    for (int i = 0; i < n; i++)
    {
        int randNum = Math.Abs(GenerateNextRandomNumber());

        randNum = min + randNum % (max + 1 +- min);
        sequence[i] = randNum;
    }

    return sequence;
}

结果应该是生成一个从最小值到最大值的数字序列。

最佳答案

不,您不能对这种类型的 RNG 使用多头:它几乎需要任意精度的数学运算。你实现的实际上看起来像 Linear Congruential Generator算法,而不是 Blum Blum Shub算法。

这是使用 .NET Core 2.2 和 Win10 x64 的代码让你开始。使用 BigInteger,我相信正确的算法和奇偶校验来获取下一个随机位。您也可以将最低有效位用于随机位。

using System;
using System.Numerics;

namespace BlumBlumSnub
{
    class Program
    {
        public static readonly BigInteger p = 3263849;
        public static readonly BigInteger q = 1302498943;
        public static readonly BigInteger m = p*q;

        public static BigInteger next(BigInteger prev) {
            return (prev*prev) % m;
        }

        public static int parity(BigInteger n) {
            BigInteger q = n;
            BigInteger count = 0;
            while (q != BigInteger.Zero) {
                count += q & BigInteger.One;
                q >>= 1;
            }
            return ((count & BigInteger.One) != BigInteger.Zero) ? 1 : 0; // even parity
        }

        public static int LSB(BigInteger n) {
            return ((n & BigInteger.One) != BigInteger.Zero) ? 1 : 0;
        }

        static void Main(string[] args)
        {
            BigInteger seed = 6367859;

            BigInteger xprev = seed;
            for(int k = 0; k != 100; ++k) {
                BigInteger xnext = next(xprev);
                int bit = parity(xnext); // extract random bit from generated BBS number using parity,
                // or just int bit = LSB(xnext);
                Console.WriteLine($"Bit = {bit}");
                xprev = xnext;
            }
        }
    }
}

关于c# - 使用 Blum Blum Shub 算法的伪随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56408677/

相关文章:

c# - 如何使用 C# 创建 appdata 文件夹

c++ - thread 不是 std c++ 的成员

python - mpmath 支持非常小的数字吗? (Python)

c# - 通过DataReader读取未知长度

c# - 如何将一种对象类型的对象深度复制到共享继承结构的另一种对象类型

c# - Serilog 电子邮件接收器 : single email at end of application

python - 随机数字列表并将它们相加

javascript - 重叠的随机 div

c++ - 将数字打印成文字

python - 试图制作一个简单的质数检查器,不会让我使用平方根。 Python 3.2