📜  生成伪随机数的加法同余法

📅  最后修改于: 2021-09-22 10:47:58             🧑  作者: Mango

Additive Congruential Method是一种线性同余生成器,用于生成特定范围内的伪随机数。该方法可以定义为:

方法:

  • 选择种子值 X 0 、模参数 m 和增量项 c。
  • 初始化所需数量的随机数以生成(例如,整数变量noOfRandomNums )。
  • 定义存储以保留生成的大小为noOfRandomNums 的随机数(此处考虑向量)。
  • 用种子值初始化向量的0索引。
  • 对于其余的索引,遵循 Additive Congruential Method 生成随机数。

最后,返回生成的随机数。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function to generate random numbers
void additiveCongruentialMethod(
    int Xo, int m, int c,
    vector& randomNums,
    int noOfRandomNums)
{
 
    // Initialize the seed state
    randomNums[0] = Xo;
 
    // Traverse to generate required
    // numbers of random numbers
    for (int i = 1; i < noOfRandomNums; i++) {
 
        // Follow the additive
        // congruential method
        randomNums[i]
            = (randomNums[i - 1] + c)
              % m;
    }
}
 
// Driver Code
int main()
{
    int Xo = 3; // seed value
    int m = 15; // modulus parameter
    int c = 2; // increment term
 
    // Number of Random numbers
    // to be generated
    int noOfRandomNums = 20;
 
    // To store random numbers
    vector randomNums(noOfRandomNums);
 
    // Function Call
    additiveCongruentialMethod(
Xo, m, c,
                               randomNums,
 noOfRandomNums);
 
    // Print the generated random numbers
    for (int i = 0; i < noOfRandomNums; i++) {
        cout << randomNums[i] << " ";
    }
 
    return 0;
}


Java
// Java implementation of the
// above approach
class GFG{
 
// Function to generate random numbers
static void additiveCongruentialMethod(
    int Xo, int m, int c,
    int []randomNums,
    int noOfRandomNums)
{
 
    // Initialize the seed state
    randomNums[0] = Xo;
 
    // Traverse to generate required
    // numbers of random numbers
    for(int i = 1; i < noOfRandomNums; i++)
    {
 
        // Follow the additive
        // congruential method
        randomNums[i] = (randomNums[i - 1] + c) % m;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Seed value
    int Xo = 3;
     
    // Modulus parameter
    int m = 15;
     
    // Increment term
    int c = 2;
 
    // Number of Random numbers
    // to be generated
    int noOfRandomNums = 20;
 
    // To store random numbers
    int []randomNums = new int[noOfRandomNums];
 
    // Function Call
    additiveCongruentialMethod(Xo, m, c,
                               randomNums,
                               noOfRandomNums);
 
    // Print the generated random numbers
    for(int i = 0; i < noOfRandomNums; i++)
    {
        System.out.print(randomNums[i] + " ");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the
# above approach
 
# Function to generate random numbers
def additiveCongruentialMethod(Xo, m, c,
                               randomNums,
                               noOfRandomNums):
 
    # Initialize the seed state
    randomNums[0] = Xo
 
    # Traverse to generate required
    # numbers of random numbers
    for i in range(1, noOfRandomNums):
         
        # Follow the linear congruential method
        randomNums[i] = (randomNums[i - 1] + c) % m
 
# Driver Code
if __name__ == '__main__':
     
    # Seed value
    Xo = 3
     
    # Modulus parameter
    m = 15
     
    # Multiplier term
    c = 2
 
    # Number of Random numbers
    # to be generated
    noOfRandomNums = 20
 
    # To store random numbers
    randomNums=[0] * (noOfRandomNums)
 
    # Function Call
    additiveCongruentialMethod(Xo, m, c,
                               randomNums,
                               noOfRandomNums)
 
    # Print the generated random numbers
    for i in randomNums:
        print(i, end = " ")
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to generate random numbers
static void additiveCongruentialMethod(
    int Xo, int m, int c,
    int []randomNums,
    int noOfRandomNums)
{
 
    // Initialize the seed state
    randomNums[0] = Xo;
 
    // Traverse to generate required
    // numbers of random numbers
    for(int i = 1; i < noOfRandomNums; i++)
    {
 
        // Follow the additive
        // congruential method
        randomNums[i] = (randomNums[i - 1] + c) % m;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Seed value
    int Xo = 3;
     
    // Modulus parameter
    int m = 15;
     
    // Increment term
    int c = 2;
 
    // Number of Random numbers
    // to be generated
    int noOfRandomNums = 20;
 
    // To store random numbers
    int []randomNums = new int[noOfRandomNums];
 
    // Function call
    additiveCongruentialMethod(Xo, m, c,
                               randomNums,
                               noOfRandomNums);
 
    // Print the generated random numbers
    for(int i = 0; i < noOfRandomNums; i++)
    {
        Console.Write(randomNums[i] + " ");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3 5 7 9 11 13 0 2 4 6 8 10 12 14 1 3 5 7 9 11

的字面量意思是false 。这些随机数被称为伪数,因为使用了一些已知的算术程序来生成。甚至生成的序列也形成了一种模式,因此生成的数字似乎是随机的,但可能不是真正的随机