CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY-TO GERNERATE RANDOM NUMBER USING LINEAR CONGRUENTIAL METHOD IN JAVA
CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY
GERNERATE RANDOM NUMBER USING LINEAR CONGRUENTIAL METHOD IN JAVA
PROGARM:
import java.util.*;
public class myRnd {
final int m,a,b;
int x;
public myRnd(){
Scanner in=new Scanner(System.in);
System.out.println("enter the value of m");
m=in.nextInt();
System.out.println("enter the value of a");
a=in.nextInt();
System.out.println("enter the value of b");
b=in.nextInt();
x = m / 2;
}
double next() {
x = (a * x + b) % m;
return (double)x / m;
}
public static void main(String[] args) throws InterruptedException {
double s, m = 0;
myRnd r = new myRnd();
Scanner scn=new Scanner(System.in);
System.out.println("enter no of rand number you want to print:");
int n=scn.nextInt();
while(n==n){
Thread.sleep(1000);
m=r.next();
System.out.println (m);
if(n%2==0){
s=m*m;
System.out.println("when no is even "+m+"is square is "+s);}
else{
s=m*m*m;
System.out.println("when no is odd"+m+"is cube value is "+s);}
}
}
}
OUTPUT:
-----------------------------------------------------------------------------------------
enter the value of m
32768
enter the value of a
25173
enter the value of b
13849
enter no of rand number you want to print:
5
1.922637939453125
when no is odd 1.922637939453125is cube value is 7.10710160061015
1.98748779296875
when no is odd 1.98748779296875is cube value is 7.8507908887211215
1.452850341796875
when no is odd 1.452850341796875is cube value is 3.0666388953916055
1.0242919921875
when no is odd 1.0242919921875is cube value is 1.0746606139418873
1.924957275390625
when no is odd 1.924957275390625is cube value is 7.1328531713997165
--------------------------------------------------------------------------------------
share your commands here⇓
Comments
Post a Comment