# Generating a Random Coprime Number

• March 15th, 2011, 01:12 AM
phleep
Generating a Random Coprime Number
I am trying to write a program, using methods, that generates a random number larger then the sum a super-increasing set of 7 numbers (each sequential number is larger then the sum of all the previous), and then creates a random integer that's coprime with this number. I have the code so it will print the 7 increasing numbers as well as the random number larger then their total sum, but I'm having trouble creating the coprime number. This is my first time working with methods, so there's probably a really simple answer that I'm just overlooking.
The part of code, the p % 2==0 part in particular:
Code Java:

```if(big % p ==0 && p % 2 ==0){ isCoPrime=false; break; }```
is written so that if the random number generated is evenly divisible by 2, the loop with break and restart. However, I am still getting even digits when the program is run. Sometimes, it doesn't even print the integer at all.

I'm aiming to make it so that if the sum is divisible by the random number or if the random number is divisible by 2, it will run the loop again. However, I feel like if either of these are true, it just ends the loop and doesn't repeat it.

I've tried tons of different logistical sequences, but none of them end up correctly :( If anybody could point me in the right direction, I would greatly appreciate it!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code Java:

``` public static void main(String[] args) { int sum = printSuperIncreasingSet(); System.out.println(); int big = generateRandomBiggerThan(sum); int p = generateCoPrimeof(big); }   public static int printSuperIncreasingSet(){ int x = 0; int sum= 0; int l=0; for(x=0;x<7;x++){ Random sevenrandomdigits=new Random(); int i=sevenrandomdigits.nextInt(50)+1; l=sum*2; sum=l+i; System.out.print((sum) + " "); } return sum; }     public static int generateRandomBiggerThan(int sum){ int big=0; Random biggerDigit = new Random(); int a=biggerDigit.nextInt(51); big=sum+a; System.out.println(big+=sum); return big; }     public static int generateCoPrimeof(int big){ int r=0; boolean isCoPrime = true; for(int i = 0; i <= 100; i++){ Random randomcoprime = new Random(); int p=randomcoprime.nextInt(100)+1; if(big % p ==0 && p % 2 ==0){ isCoPrime=false; break; } } if ( isCoPrime){ System.out.println(p); } return p; } }```