You are on page 1of 4

# Mahesh Ashok Abnve 500862 Batch III

## Title: Generating Random numbers using LCM & CLCM methods

Code
package rngenerationsdemo;

import java.io.*;

/**
* @author Mahesh
*/
public class Main {

## public static void main(String[] args) throws Exception {

int c, k, choice;

while (true) {
System.out.println(" Generating pseudo random numbers using LCM &
CLCM methods");
System.out.println("Which method to use:");
System.out.println("1. LCM");
System.out.println("2. CLCM");
System.out.println("3. Exit");
System.out.println("----------------------------");
System.out.print("Enter choice:");

switch (choice) {
case 1: {
System.out.print("How many random numbers to generate: ");

int m, a;
double[] X = new double[k+1];
double[] R = new double[k];

System.out.println("Enter input");
System.out.print("Seed (Xo): ");
System.out.print("Multiplier (a):");
System.out.print("Modulus (m):");
System.out.print("Increment (c):");

## for (int i = 0; i < 3; i++) {

X[i + 1] = (a * X[i] + c) % m;
}

## for (int j = 1; j < 4; j++) {

R[j-1] = X[j] / m;
System.out.println("Random number " + j + ": " + R[j-1]);
}

break;
}
case 2: {
System.out.print("How many random numbers to generate: ");

## int a[] = new int[k];

int m[] = new int[k];
double[][] Xig = new double[k + 1][k]; //individual generator
double[] Xcg = new double[k]; //combined generator
double[] R = new double[k];

System.out.println("Enter input");
System.out.println("Seeds for generators: ");
for (int i = 0; i < k; i++) { //accepting seed values X10,X20,
X30
System.out.print(" X" + i + "0: ");
}

System.out.println("Multipliers:");
for (int i = 0; i < k; i++) {
System.out.print(" a" + i + ":");
}

System.out.println("Moduli:");
for (int i = 0; i < k; i++) {
System.out.print(" m" + i + ":");
}

System.out.print("Increment:");

## //generating individual generator

for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
Xig[i + 1][j] = (a[j] * Xig[i][j] + c) % m[j];
}
}

## //generating combined generator

double Xtemp; //Xtemp is the sum term in Xcg
System.out.println();
for (int i = 1; i <= k; i++) {
Xtemp=0;
for (int j = 0; j < k; j++) {
Xtemp = Xtemp + Math.pow(-1, j) * Xig[i][j];
}
Xcg[i-1] = Xtemp % (m[0] - 1);
if (Xcg[i-1] > 0) {
R[i - 1] = Xcg[i-1] / m[0];
} else if (Xcg[i-1] == 0) {
R[i - 1] = (m[0] - 1) / m[0];
}
System.out.println("Random number " + i + ": " + R[i - 1]);
}
break;
}
case 3: {
System.out.println("\nExiting...");
System.exit(0);
break;
}
}
}
}
}

Output
Gaenerating pseudo random numbers using LCM & CLCM method
Which method to use:
1. LCM
2. CLCM
3. Exit
----------------------------
Enter choice:1
How many random numbers to generate: 3
Enter input
Seed (Xo): 37
Multiplier (a):7
Modulus (m):100
Increment (c):29
Random number 1: 0.88
Random number 2: 0.45
Random number 3: 0.44

## Generating pseudo random numbers using LCM & CLCM method

Which method to use:
1. LCM
2. CLCM
3. Exit
----------------------------
Enter choice:2
How many random numbers to generate: 3
Enter input
Seeds for generators:
X00: 100
X10: 300
X20: 500
Multipliers:
a0:157
a1:146
a2:142
Moduli:
m0:32363
m1:31727
m2:31657
Increment:0

## Random number 1: 0.34956586225010045

Random number 2: 0.08383030003398943
Random number 3: 0.05144764082439823

## Generating pseudo random numbers using LCA method

Which method to use:
1. LCM
2. CLCM
3. Exit
----------------------------
Enter choice:3

Exiting...