lundi 17 août 2015

Implementing a linear congruential generator in java

I need to implement a Linear Congruential Generator (LCG) in Java. It should be doing the following:

 Allow the user to select values for Fact, Const, M and a starting Seed number.

 Generate 1000 random numbers between 0 and 9 using the provided values.

 Store the number of times each number is generated in an array.

 Output the distribution of the numbers at the end.

What I have done so far is not doing the required job. I need help with the second, third and fourth step. Thank you.

Below is what I have done so far:

package com.company;

import java.util.*;

public class Main {

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 Const: ");
    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];

    Random randomGenerator = new Random(seed);
    for (int idx = 0; idx < 1000; idx++){
        int randomInt = randomGenerator.nextInt(modulus+1);
        arrayBox[randomInt] = arrayBox[randomInt] + 1;
    }

    for(int i =0; i< 10; i++){
        System.out.println("amount of "+i+" is "+arrayBox[i]);
    }

}

}




Aucun commentaire:

Enregistrer un commentaire