5

I am trying to generate a random number larger than and smaller than the previous random number, but 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 do I make sure it doesn't generate a random number that it already has generated? The computer gets 10 tries to guess the number that was generated, but I want to make it logical in the way that it wouldn't generate one that it already knows is wrong.

Sam Mitchell
  • 194
  • 2
  • 18

4 Answers4

3

What 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 that there are no duplicates. Of course, if MAX has value 1 this may take a while.

One advantage of this approach is that the numbers should be pretty well distributed over 0 to MAX. If you use ranges directly then you have to deal with upper and lower bounds.

Of course this approach can easily be extended to work over ranges as well, as long as the maximum amount of values is (significantly) higher than the amount of numbers in the list (in this case just 3).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I don't think that is what I'm looking for, for my specific program, however I understand where you're coming from. But is there a more basic way of making a list, and making sure no dupes are generated? – Sam Mitchell Oct 31 '14 at 01:24
  • Sure, you can create a list and when adding the numbers, check if they are unique. That is of course fine for a small list. Of course, using a `TreeSet` would even be better, but I didn't want to spook you completely. – Maarten Bodewes Oct 31 '14 at 01:28
  • I am definitely not an advanced Java programmer, so is there a basic way of checking for dupes and making sure the guess does not equal a dupe – Sam Mitchell Oct 31 '14 at 01:31
  • Eh, yes, there is this bag kind of thing that may not contain dupes. It's called a Set. Collections are the modern way to approach OOP and Java. – Maarten Bodewes Oct 31 '14 at 01:34
  • I'll look it up and reply if I need any more help. Thanks! – Sam Mitchell Oct 31 '14 at 01:36
  • Ok so I used a set and then added the contains(guess) to the while statement checking if the guess is above or under the previous guess, but it still dupes – Sam Mitchell Oct 31 '14 at 15:24
  • (Hey, missing a comment that I thought I already posted when traveling). The above should not return any dupes, I've tested it and a `Set` certainly [*cannot* contain dupes](http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#add(E)): "Adds the specified element to this set if it is not already present (optional operation)." The above should be all you need. – Maarten Bodewes Oct 31 '14 at 23:03
  • I understand that yours should work, however I don't want to jump into it advance stuff so quickly. What I tried is I made a set called numberList and each time a random guess was generated I added that guess to the set. Within the while loop with the condition that the new guess can't be less then if too low or greater than if too high, I also put it must not equal a number contained in that set. However when I ran it, it generated 9 three separate times (not consecutive). I then tried to make the max = max -1 if the guess was too high, and min = min + 1 if too low. This didn't work either – Sam Mitchell Oct 31 '14 at 23:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64071/discussion-between-owlstead-and-mr-wizerman69). – Maarten Bodewes Oct 31 '14 at 23:12
0

If the computer player receives information that its guess is either too low or too high (or correct), you need to keep track of the range of possible values that the correct value could be. Initially, this is the entire range, min..max. Once you guess a value that is incorrect, 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!!
}

Now you you always have the correct range for valid guesses between min and max.

Now, your logic to generate random numbers within the range seems to be a bit off. This answer to the question linked in @Solver's answer has the correct logic:

guess = (int)( (max - min) * Math.random() + 1) + min;
Community
  • 1
  • 1
beaker
  • 16,331
  • 3
  • 32
  • 49
-1

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: Misinterpreted the question.

habitats
  • 2,203
  • 2
  • 23
  • 31
-1

You can specify the minimum andere maximum values when getting a random nummer as shown here: How do I generate random integers within a specific range in Java?

Community
  • 1
  • 1
Solver
  • 51
  • 1
  • 9