How to make a minimum ceiling and a maximum ceiling in a random number?

Asked

Viewed 529 times

1

I am developing an application in Java and in my controller I have a function that generates random numbers. Example:

In my first column I want you to manage from 0 to 8, so far OK.

In the second column I want you to manage 9 to 17, then in this case I can’t do it because I can’t determine which number x will start, because by default it counts with the beginning at 0.

I mean, I want you to run 0-9, then 9-17, then 18-25 and so on...

Follow my code below:

public void grupoDeSete(){
        Random random = new Random();

        /*=====ACTIVITY PLUS=======*/
        int generated1_plus = random.nextInt(9);
        int generated2_plus = random.nextInt(17 - 9) + 10;
        int generated3_plus = random.nextInt(24 - 18) + 20;

        /*Grupo de 7 números*/
        String sevenGroup = String.valueOf(generated1_plus
                + " - "
                + generated2_plus
                + " - "
                + generated3_plus);

        //Resultado
        setSecond(sevenGroup);
    }


    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

How can I do?

  • Are these groups 0-9 and 9-17? Or would it be 0-8 and 9-17? Or still 0-9 and 10-17? Also, you speak 18-25, but your code generates 20-25. Your code also generates 10-18. I can’t understand what logic you want for those numerical ranges and without that you can’t answer the question.

  • So I tried to make a "scheme" so that it generates any number up to 18... then this 17 - 9 = 8(it generates from 0 to 8) and then add + 10 giving a value of 10 up... only that wanted some form that determines my minimum and that I can get out of this formula... something that is (start between x and y) as the example

  • Yes, but why 18? I’m not asking about the formula, I’m asking what are the desired tracks. Knowing the tracks, finding the formula is easy.

  • is why I wanted 18 anyway for this

2 answers

4


You can do something like this:

public class Sorteio {
    private static final Random RND = new Random();

    public static int sortear(int min, int max) {
        return RND.nextInt(max - min + 1) + min;
    }

    public static int[] sortearSete() {
        return new int[] {
            sortear(0, 9),
            sortear(10, 18),
            sortear(19, 26),
            sortear(27, 33),
            sortear(34, 39),
            sortear(40, 44),
            sortear(45, 48)
        };
    }
}

The method sortear(int, int) gives you two numbers within a track. For example, sortear(20, 30) gives any number from 20 to 30, including the 20 itself and the 30 itself. And then the method sortearSete() gives you these 7 numbers. It is important that you adjust the numerical ranges within this method for the tracks you want.

I put the following tracks: a track containing 10 numbers from 0 to 9; a track containing 9 numbers from 10 to 18; a track containing 8 numbers from 19 to 26; a track containing 7 numbers from 27 to 33; a track containing 6 numbers from 34 to 39; a range containing 5 numbers from 40 to 44 and a range containing 4 numbers from 45 to 48.

  • Ahh, I get it... but so when I return this value to show on the screen I would need to convert to string, when I convert it appears a whole messy way... how would I show the result?

0

You must call nextFloat or nextDouble, that return decimals between zero and one and make the transformations you need. More or less like below:

(int) min + (random.nextFloat() * (max - min));

This will return you an int with values between min and max.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.