I’m trying to set up a simple addition sequence using the random generator. The number has to be an int.
Here is an example of what I’m trying to accomplish:
(4, 10, 16, 22, 28, ?) : the user must input the next number in the sequence. (it is 34 because each number is 6 numbers apart from one another.)
The user is given random numbers from the generator, but the 2 numbers next to each other must have the same value for the distance apart as the next two. Ex: 10-4 = 6, 16-10 = 6, 22-16 = 6.
private static void simpleAdditionSequence() {
int num1, num2, num3, num4, num5, numUser;
Random generator = new Random();
num1 = generator.nextInt(2147483647);
System.out.println(num1);
}
The problem I’m having is figuring out how to create num2. If I just use another generator it could be less than num1, and it needs to have the same value for its distance apart as the rest of the numbers.
(Note – I used 2147483647 so I can generate random INTEGERS.
Only two numbers in your problem are actually random — the first number and the difference between numbers.
Generate two random integers; one for the base, one for the step, and be content.
(One thing to look out for — you should only ever create one random-number-generator in your program. Re-use that generator every time you need random numbers. If you create a new generator every time you need a random number, your numbers will not have the randomness properties you desire.)