📜  生成伪随机数的线性同余方法

📅  最后修改于: 2021-04-29 02:17:45             🧑  作者: Mango

线性同余方法是一类伪随机数生成器(PRNG)算法,用于生成特定范围内的类随机数序列。此方法可以定义为:

对于a = 1,这将是加法全等法。
对于c = 0,它将是乘法同余方法。

方法:

  • 选择种子值X 0,模数参数m,乘数项a和增量项c。
  • 初始化所需数量的随机数以生成(例如,整数变量noOfRandomNums )。
  • 定义一个存储区,以保持生成的随机数(此处为vector )的大小为noOfRandomNums
  • 用种子值初始化向量的0索引。
  • 对于其余的索引,请遵循线性同余方法来生成随机数。

最后,返回随机数。
下面是上述方法的实现:

C++
// C++ implementation of the
// above approach 
  
#include  
using namespace std; 
  
// Function to generate random numbers 
void linearCongruentialMethod( 
    int Xo, int m, int a, 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 linear congruential method 
        randomNums[i] 
            = ((randomNums[i - 1] * a) + c) % m; 
    } 
} 
  
// Driver Code 
int main() 
{ 
    int Xo = 5; // Seed value 
    int m = 7; // Modulus parameter 
    int a = 3; // Multiplier term 
    int c = 3; // Increment term 
  
    // Number of Random numbers 
    // to be generated 
    int noOfRandomNums = 10; 
  
    // To store random numbers 
    vector randomNums( 
        noOfRandomNums); 
  
    // Function Call 
    linearCongruentialMethod( 
        Xo, m, a, 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 appraoch
import java.util.*;
  
class GFG{
  
// Function to generate random numbers
static void linearCongruentialMethod(int Xo, int m, 
                                     int a, 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 linear congruential method
        randomNums[i] = ((randomNums[i - 1] * a) + c) % m;
    }
}
  
// Driver code
public static void main(String[] args)
{
      
    // Seed value
    int Xo = 5; 
      
    // Modulus parameter
    int m = 7; 
      
    // Multiplier term
    int a = 3; 
      
    // Increment term
    int c = 3; 
      
    // Number of Random numbers
    // to be generated
    int noOfRandomNums = 10;
      
    // To store random numbers
    int[] randomNums = new int[noOfRandomNums];
      
    // Function Call
    linearCongruentialMethod(Xo, m, a, 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 offbeat


Python3
# Python3 implementation of the
# above approach
  
# Function to generate random numbers
def linearCongruentialMethod(Xo, m, a, 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] * a) +
                                         c) % m
  
# Driver Code
if __name__ == '__main__':
      
    # Seed value
    Xo = 5 
      
    # Modulus parameter
    m = 7 
      
    # Multiplier term
    a = 3 
      
    # Increment term
    c = 3 
  
    # Number of Random numbers
    # to be generated
    noOfRandomNums = 10
  
    # To store random numbers
    randomNums = [0] * (noOfRandomNums)
  
    # Function Call
    linearCongruentialMethod(Xo, m, a, 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 appraoch
using System;
  
class GFG{
  
// Function to generate random numbers
static void linearCongruentialMethod(int Xo, int m, 
                                     int a, 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 linear congruential method
        randomNums[i] = ((randomNums[i - 1] * a) + c) % m;
    }
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Seed value
    int Xo = 5; 
      
    // Modulus parameter
    int m = 7; 
      
    // Multiplier term
    int a = 3; 
      
    // Increment term
    int c = 3; 
      
    // Number of Random numbers
    // to be generated
    int noOfRandomNums = 10;
      
    // To store random numbers
    int[] randomNums = new int[noOfRandomNums];
      
    // Function call
    linearCongruentialMethod(Xo, m, a, c,
                             randomNums,
                             noOfRandomNums);
      
    // Print the generated random numbers
    for(int i = 0; i < noOfRandomNums; i++)
    {
        Console.Write(randomNums[i] + " ");
    }
}
}
  
// This code is contributed by sapnasingh4991


输出:
5 4 1 6 0 3 5 4 1 6

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