0

My assignment:

A simple random generator is obtained by the formula = ( ⋅ + )%. New “random” numbers are then generated by setting to and repeating the process. Write a method that asks the user to enter a value for , , and . Your method should return an array of integers that contain the first 25 “random” values generated by this formula.

So far this is what I have, but for some reason my code is not printing the array of random 25 numbers

public static void main(String[] args){
        Scanner theInput = new Scanner(System.in);
        System.out.println("Enter a value for r: ");
        int r = theInput.nextInt();
        System.out.println("Enter a value for a: ");
        int a = theInput.nextInt();
        System.out.println("Enter a value for b: ");
        int b = theInput.nextInt();
        System.out.println("Enter a value for m: ");
        int m = theInput.nextInt();

        System.out.println(random(r,a,b,m));

    }

    public static int[] random(int r, int a, int b, int m){
        String num = "";
        int numberArray[] = new int [25];
        for (int i = 0; i < numberArray.length; i++) {
            int answer = (a*r+b)%m;
            numberArray [i] = answer;
        }
        for(int i=0;i<numberArray.length;i++){
            System.out.println(numberArray[i]);
        }
        System.out.println();
        return numberArray; 
    }

This is what is printing:

258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258
258

[I@55f96302

Can someone help me to fix the problem?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Mary
  • 3
  • 6
  • 2
    You haven't implemented this step: `New “random” numbers are then generated by setting to and repeating the process.` – Erwin Bolwidt Sep 10 '16 at 00:59
  • You've not implemented it the way it calls for in the instructions. Read them again and investigate your code for the problem. The output is a dead giveaway. – ChiefTwoPencils Sep 10 '16 at 00:59
  • So I fixed my code – Mary Sep 10 '16 at 01:50
  • public static int[] random(int r, int a, int b){ String num = ""; int numberArray[] = new int [25]; for (int i = 0; i < numberArray.length; i++) { int answer = (a*r+b); numberArray [i] = answer; r = answer; } for(int i=0;i – Mary Sep 10 '16 at 02:03
  • But it keeps printing the memory address of the array. Is there a way to fix this? – Mary Sep 10 '16 at 02:06

2 Answers2

1

Passing an array to System.out.println() will print out the "memory address" of the array. You can use Arrays.toString() to get a nicely formatted String of the array contents:

System.out.println(Arrays.toString(random(r,a,b,m)));
Matthew Diana
  • 1,106
  • 7
  • 14
1
  1. You get 25 same numbers is because you are using the same r for the same formula 25 times!

Instead, according to the assignment requirements, you should update the r as the newly generated random number and use it to generate the next random number!

  1. You should use a for loop to print an array instead of System.out.println(numberArray), see this for more explanation.

For your reference:

public static int[] random(int r, int a, int b, int m) {
    String num = "";
    int numberArray[] = new int [25];
    for (int i = 0; i < numberArray.length; i++) {
        int answer = (a * r + b) % m;
        numberArray [i] = answer;
        r = answer;                // you should set r as the answer and use it for the next random number
    }
    return numberArray;
}

public static void main(String[] args) {

    Scanner theInput = new Scanner(System.in);
    System.out.println("Enter a value for r: ");
    int r = theInput.nextInt();
    System.out.println("Enter a value for a: ");
    int a = theInput.nextInt();
    System.out.println("Enter a value for b: ");
    int b = theInput.nextInt();
    System.out.println("Enter a value for m: ");
    int m = theInput.nextInt();

    int[] numberArray = random(r, a, b, m);

    for(int i = 0; i < numberArray.length; i++){
        System.out.println(numberArray[i]);
    }
}
Community
  • 1
  • 1
Tan Li Hau
  • 1,984
  • 17
  • 24