title: How to keep a random variable throughout?
body: <p>I apologize in advance for my English, not my first language.</p>
  
  <p>Brief description of program: Generate random first term in arithmetic sequence and generate random difference to get next term. Then ask user to put in the sixth term.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Project{
     static int diff;
  
     public static int calcSeq(int num){
        return num+diff;
     }
  
     public static void main(String[] args){
  
        Scanner keyboard = new Scanner(System.in);
  
        int diff = (int)(Math.random()*20)+1;
        int term1 = (int)(Math.random()*10)+1;
        int sum = term1;
        int ans = 0;
  
        System.out.println(term1);
  
        for (int i = 1; i&lt;=4; i=i+1){
           int nextTerm = calcSeq(term1);
           sum = sum + nextTerm;
  
           System.out.println(sum);
        }
  
        int term6 = sum + diff;
        System.out.println(term6);
        System.out.print("Enter the sixth term of this arithmetic sequence: ");
        ans = keyboard.nextInt();
  
        if (ans == term6){
           System.out.println("That is the correct answer.");
        }
        else if (ans != term6){
           System.out.println("That is incorrect. Try again.");
        }
     }
  }
  </code></pre>
  
  <p>For the first five terms the difference remains the same and is correct arithmetic sequence. But then when get to sixth term the difference changes and the sequence is no longer correct. For example correct sequence could be 2,4,6,8,10,12. Here the difference is 2. But when the program runs it goes like 2,4,6,8,10,15 which is not correct. So my question is how to keep the random difference constant throughout?
  Thanks</p>