Generate a random number greater or less than the previous random number

I am trying to generate a random number greater and less than the previous random number, but I cannot figure out how.

I have this so far:

number = (int)( max * Math.random() ) + min;
guess = (int)( max * Math.random() ) + min;
if (guess<number)
   {
      guess = (int)( max * Math.random() ) + min;
      System.out.println(guess);
    }
else if (guess>number)
 {
      guess = (int)( max * Math.random() ) + min;
     System.out.println(guess);
 }

      

UPDATE: How can I make sure it doesn't generate the random number it has already generated? The computer gets 10 tries to guess the number that was generated, but I want to make it logical in the sense that it won't generate one that it already knows is wrong.

+3


source to share


4 answers


How about ordering a list of random numbers ...

public static void method2() throws Exception {
    Random rng = new SecureRandom();
    Set<Integer> numbers = new HashSet<>();
    while (numbers.size() < 3) {
        // number only added if not already present in Set, set values are unique
        numbers.add(rng.nextInt(MAX));
    }
    List<Integer> numberList = new ArrayList<>(numbers);
    Collections.sort(numberList);
    // lower random at index 0, mid at index 1
    // you can guess where the other one is hiding 
    System.out.println(numberList);
}

      

I put them in a set first to make sure there are no duplicates. Of course, if the MAX

value is 1, it may take a while.



One of the advantages of this approach is that the numbers should be well distributed from 0 to MAX. If you are using ranges directly, you need to deal with upper and lower bounds.

Of course, this approach can be easily extended to work on ranges, if the maximum amount of values โ€‹โ€‹(significantly) exceeds the number of numbers in the list (in this case, only 3).

+2


source


This will always return numbers like this: less <= number <= more

  number = (max * Math.random()) + min;
  less = (number * Math.random()) + min;
  more = ((max - number) * Math.random()) + number;

      



edit: Unclear question.

0


source


You can specify the minimum and maximum values โ€‹โ€‹when getting a random number, as shown here: How to generate random integers in a specific range in Java?

0


source


If the computer player receives information that its guess is too low or too high (or correct), you need to keep track of the range of possible values โ€‹โ€‹that might be correct. Initially, this entire range min..max

. Once you guess the wrong value, you move either min

, or max

:

if (guess<number)
{
   min = guess + 1; // Don't guess any more number <= guess
}
else if (guess>number)
{
   max = guess - 1; // Don't guess any more number >= guess
}
else
{
   // I WIN!!
}

      

You now always have the correct range for acceptable guesses between min

and max

.

Now your logic for generating random numbers within a range seems a little off. This answer to a question linked to @ Solver's answer has the correct logic:

guess = (int)( (max - min) * Math.random() + 1) + min;

      

0


source







All Articles