2

I have one problem in my Linear Congruential Generator. I have two Modulus. If I replace or remove any one of them, I don't get any output, except for the counter which initiates at the end but it's pretty useless if there's no output to process using the counter.

I need the application to output the numbers in a range from 0 to 9 and then output the distribution of the numbers at the end. Here is my code:

public static void main(String[] args) {
  int fact;
  int constant;
  int modulus;
  int seed;

  Scanner scan = new Scanner(System.in);

  System.out.println("Input fact: ");
  fact = scan.nextInt();

  System.out.println("Input constant: ");
  constant = scan.nextInt();

  System.out.println("Input modulus: ");
  modulus = scan.nextInt();

  System.out.println("Input seed: ");
  seed = scan.nextInt();

  int[] arrayBox;
  arrayBox = new int[10];

  for (int i = 0; i < 10; i++) {

    seed = (seed * fact + constant) % modulus; // First modulus

    System.out.println(seed);
    arrayBox[seed % 10] = arrayBox[seed % 10] + 1; // Second modulus
  }

  for (int i = 0; i < 10; i++) {
    System.out.print(+(100 * arrayBox[i] / 10) + "% ");
  }
}

Is there any way around the second modulus and still get my output and counter working in the range that I want (0 to 9)?

double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

1

I'm having a little trouble following what you're trying to do, but I think what you want is, at the end:

System.out.println(i + ":\t" + (100*arrayBox[i]/10) + "%");

Note that this program is not saving the actual seed values anywhere, so you will not be able to view them.

Here's a sample run of this program with some reasonable values for the input parameters:

Input fact: 
257
Input constant: 
31
Input modulus: 
64
Input seed: 
89
56
23
54
21
52
19
50
17
48
15
0:  10%
1:  10%
2:  10%
3:  10%
4:  10%
5:  10%
6:  10%
7:  10%
8:  10%
9:  10%
durron597
  • 31,968
  • 17
  • 99
  • 158