首页 > 解决方案 > 使用 Blum Blum Shub 算法的伪随机数生成器

问题描述

我们需要在伪随机数生成器中实现 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;
}

结果应该是生成从 min 到 max 的数字序列。

标签: c#randomnumbersgenerator

解决方案


不,您不能对这种类型的 RNG 使用 long:它几乎需要任意精度的数学运算。你实现的实际上看起来像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;
            }
        }
    }
}

推荐阅读