简体   繁体   中英

How do I generate 1000 random numbers between 0 and 9?

How do I generate 1000 random numbers between 0 and 9 using values provided by the user?

  1. The program needs to allow the user to select the values for fact, constant, modulus and a starting seed number.

  2. It should generate 1000 random numbers between 0 and 9 using those values.

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

  4. Output the distribution of the numbers at the end.

Here is what I've done till now but I've only got the user to input the information till now, so, I'm very far from having achieved number 2, 3 and 4.

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

    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 M: ");
    modulus = scan.nextInt();

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

    int [] arrayBox;
    arrayBox = new int [10];
    for (int i=0; i<10; i++){
        arrayBox[i]=0;
      }

    int noOfNumberGenerated=0;
    while (noOfNumberGenerated<1000){
        seednumber=(((fact*seednumber)+constant)%modulus);
}

First there is no need for this part:

for (int i=0; i<10; i++){
        arrayBox[i]=0;
}

the array will by default be populated by 0's.

For part 2 and 3:

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

Part 4:

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

--edit-- As response to your comment, the Random class in Java is in itself an implementation of LGC and considered as one of the better ones. The constants are hardcoded as

fact = 25214903917
constant = 11

These numbers have been chosen after lots of testing and I'm sure you can find a lot of documentation about it on the internet.

In your case you have 2 options. Either force Random to use your constants or write it yourself. Option 1 will give you a weaker version of the Random class but which will still be 'stronger' then option 2.

For 1:

public class RandomCustom extends Random{

    //You have to Override this aswell.
    @Override
    synchronized public void setSeed(long seed) {
        this.seed.set(initialScramble(seed));
        haveNextNextGaussian = false;
    }

    @Override
    protected int next(int bits){
        long oldseed, nextseed;
        AtomicLong seed = this.seed;
        do {
            oldseed = seed.get();
            nextseed = (oldseed * fact + constant) & modulus;
        } while (!seed.compareAndSet(oldseed, nextseed));
        return (int)(nextseed >>> (48 - bits));
    }
}

After this just change Random from the code at the top in RandomCustom and you are ready.

In my opinion tho, why try to reinvent the wheel instead of using something that has been proven to be solid and working

If you are planning to use the Random class, you do not need all these input parameters. All you need is a long seed and the modulo to use for the generation (and if required the number of generations):

import java.util.*;

public class RandomNumbers {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Input Long Seed: ");
        long seed = scan.nextLong();

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

        System.out.println("Number of Iterations: ");
        int iterations = scan.nextInt();

        int[] arrayBox =new int[modulus];

        Random random = new Random(seed);

        for (int i=0;i<iterations;i++){
            arrayBox[random.nextInt(modulus)]++;
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM