lab9

#include <iostream>

#include <cmath>

using namespace std;

// Function to compute GCD (Greatest Common Divisor)

int gcd(int a, int b) {

if (b == 0)

return a;

return gcd(b, a % b);

}

// Blum Blum Shub Random Number Generator

void blumBlumShub(int seed, int p, int q, int numRandomNumbers) {

int M = p * q; // Compute modulus (M)

int x = seed % M; // Ensure seed is within range

cout << "Generated Pseudo-random numbers: \n";

// Generate 'numRandomNumbers' random numbers

for (int i = 0; i < numRandomNumbers; i++) {

x = (x * x) % M; // BBS formula

cout << x << " "; // Output random number

}

cout << endl;

}

// Driver function

int main() {

int seed, numRandomNumbers;

// Two large prime numbers p and q (must be congruent to 3 mod 4)

int p = 499; // Prime 1

int q = 547; // Prime 2

// Ensure p and q are congruent to 3 mod 4

if (p % 4 != 3 || q % 4 != 3) {

cout << "Error: p and q must be prime numbers where p ≡ 3 mod 4 and q ≡ 3 mod 4" << endl;

return 1;

}

cout << "Enter seed value (should be coprime with " << p * q << "): ";

cin >> seed;

// Ensure the seed is coprime to M

if (gcd(seed, p * q) != 1) {

cout << "Error: Seed must be coprime with " << p * q << endl;

return 1;

}

cout << "Enter the number of random numbers to generate: ";

cin >> numRandomNumbers;

// Generate and print pseudo-random numbers

blumBlumShub(seed, p, q, numRandomNumbers);

return 0;

}

Comments

Popular posts from this blog

lab7

lab12(euler)

lab6b