mercredi 24 juillet 2019

Java Random Class nextInt() to return numbers in a pattern

I'm trying to use the Random class to display a series of random numbers from the set 6, 10, 14, 18, 22 by using the nextInt method and without using an array. I know that I can generate random numbers from 6-22 using the method, but is there any way to specify that it should go in increments of 4?

This code displays 100 random numbers in the range 6-22:

    Random rand = new Random();
    rand.setSeed(40);
    Random rand1 = new Random(40);
    for(int i = 0; i < 100; i++)    {
        System.out.println(rand.nextInt((22 - 6) + 1) + 6);
    }

Ignore the seed value, I need it to display the same series every time the program is run.




Aucun commentaire:

Enregistrer un commentaire