Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Donato Braico
- * @version 1.0
- */
- public class RandomNumber {
- static int modp;
- static int modq;
- static int p;
- static int q;
- static long n;
- static long seme;
- static long x;
- static long[] seq;
- public static void init (int num) {
- //Default
- modp=4;
- modq=4;
- p=4;
- q=4;
- seme=0;
- seq=new long[num];
- }
- /**
- * This method generates pseudo-random numbers using the Blum Blum Shub algorithm
- * @param num - numbers generated
- */
- public static void generate(int num) {
- init(num);
- //1 step
- while (modp!=3 || modq!=3) {
- p=(int)Math.round(Math.random()*100000000);
- q=(int)Math.round(Math.random()*100000000);
- modp=p%4;
- modq=q%4;
- }
- n=Math.abs(q*p);
- //2 step
- while (seme < 1 || seme > (n-1) || !(coprimi(seme,n))) {
- seme=Math.round(Math.random()*100000000);
- }
- x=(long)Math.pow(seme, 2)%n;
- //3 step
- seq[0]=x;
- for (int i=1; i<num; i++) {
- seq[i]=(long)Math.pow(seq[i-1], 2)%n;
- long z=seq[i]%100;
- System.out.println(z);
- }
- }
- /**
- * @param a
- * @param b
- * @return true - if the MCD between a and b is 1;
- */
- private static boolean coprimi (long a, long b) {
- boolean result=false;
- long resto;
- if (b==0) {
- if (a==1) {
- result=true;
- }
- }
- else {
- resto=a%b;
- if (resto==0) {
- if (b==1) {
- result=true;
- }
- }
- else {
- return(coprimi (b,resto));
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment