Link to home
Start Free TrialLog in
Avatar of Gesus
Gesus

asked on

random numbers

i want an array or 4 random numbers.

then i want the user to input 4 numbers and compare them with the randomly generated ones. (sort of like mastermind, if u've played the game)

i understand that i can use the the random() method from the standard Math class.  does that guarentee the random things will be numbers (i'm guessing the fact that its is math class means yes)

is there anyone who has mastermind style code that i could adapt. (i don't want the whole white pin, black pin thing!)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gesus
Gesus

ASKER

whats the final int max bit do?  
That'll allow you to decide what your range is:

"Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence."

MAX would be that "specified value".
Avatar of Gesus

ASKER

oh, so i get it.  so the max number would be 9999, since its exclusive, right?
Yes. So to be a little more intuitive, you should really do something like:

final int MAX = 10000 + 1;

which will make it a bit more obvious you mean 10000
Avatar of Gesus

ASKER

oh, so i get it.  so the max number would be 9999, since its exclusive, right?
Avatar of Gesus

ASKER

whoops, sorry. didn't mean to repost then!
Hi,

Another thing you should know is although it's a random method. The method ISNOT completely random, infact it's still following a mathematical formula.

So if you want a better random generator then you'll have to some research into Probability & Randomness Theory or pseudo codes. Then again if you don't really care, then stick to random().

That's all, quite irrelevant really.
Avatar of Gesus

ASKER

umm, thanks!
i think random will be enough.

but how can i compare the input from the user with the items in the array.  and repeat until they get it right
Hi,

The problem with random() is it'll generate a patern of random numbers and after that it will keeping coming with this same patern again and again...if you keep on running it.

So the only way is to "catch" when the same patern is repeating itself again and call another random() until you have the desired match.

Bye.
import java.io.*;
import java.util.Random;
import java.util.Arrays;

public class NumberGuess {

 public static void main (String[] args) {

   int guess;
   final int MAX = 5 + 1;
   int[] numbers = new int[4];
   Random rand = new Random();
   for (int i = 0;i < 4; i++) {
    numbers[i] = rand.nextInt(MAX);
    //System.out.println(numbers[i]);
   }
   // Array must be sorted before searching
   Arrays.sort(numbers);

   while (true) {
     try {
       System.out.print("Enter your guess: ");
       java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
       guess = Integer.parseInt(in.readLine());
       if (Arrays.binarySearch(numbers, guess) >=0) {
         System.out.println("You guessed right!");
         in.close();
         break;
       }

     }
     catch(Exception e){
       e.printStackTrace();
     }
   }
 }
}
Hi,

The problem with random() is it'll generate a patern of random numbers and after that it will keeping coming with this same patern again and again...if you keep on running it.

So the only way is to "catch" when the same patern is repeating itself again and call another random() until you have the desired match.

Bye.
No, it won't
Avatar of Gesus

ASKER

hi cehj.

// Array must be sorted before searching
   Arrays.sort(numbers);


why does the array need to be sorted?

does this mean that if the array is 4321 then the sorting will make it in numerical sort (1234) ?
>>why does the array need to be sorted?

Because the binarySearch method that is used to see if the guess is in your array will not work otherwise.

>>does this mean that if the array is 4321 then the sorting will make it in numerical sort (1234) ?

Yes, but of course that does not affect your program,unless it isn't sorted in which case it won't work in its present form.
Is that all clear now Gesus?
Avatar of Gesus

ASKER

not really.

the number i put in isn't going to be ordered.


the random number could be 3875

if its sorted it would become 3578

therefore if i input 3578, it would say correct, right?

but i don't want that.  i want it to match the random number, so i can compare.

random[0] == myguess[0] and so on.  
I think you're misunderstanding what's going on. You want an array of four random numbers according to your requirement. The fact that these four numbers are sorted has no connection with the guess whatsoever.

If you uncomment this line


   //System.out.println(numbers[i]);

you'll see what numbers you've got
At the moment, in my code example the numbers range from 0 to 5 inclusive, so if the generated numbers are say

0
1
4
5

and you user guesses 2, he'll be put in a loop to guess again until he guesses *one* of those four generated numbers.
Avatar of Gesus

ASKER

the guess should also be 4 numbers long, and the numbers should be 0 to 9.  see what i mean?  sorry if i haven't explained it well
Avatar of Gesus

ASKER

eg, random generated number are 2 3 4 6

user inputs 2364 WRONG
user inputs 2436 WRONG
user inputs 2346 RIGHT

type thing
>>the guess should also be 4 numbers long, and the numbers should be 0 to 9.  see what i mean?  sorry if i haven't explained it well

In that case, why don't you generate just one random number between 1000 and 9999 and guess one number?
Avatar of Gesus

ASKER

i want to be able to say "you guessed 2 correctly", or "you got 4" etc
Well, i'd have thought you'd be able to derive the correct code from what i've posted, but in case you can't, please post your exact and precise specification. You *haven't* been clear ;-)
Avatar of Gesus

ASKER

right, i'll try to be more clear.
have you ever played mastermind?
in that game if the random number was 2345
and you guess 2354 it would say you have 2 in the right places and 2 in the wrong places.

i want something like that, but not the 2 in the wrong places.  i want the program just to notice the entries that are in the right place
Avatar of Gesus

ASKER

actually, its probably going to be easier to understand if u write it as a mastermind thing, and i can change it.

basically, i've been playing with your program, and when i get rid of the array sort thing, it won't work. (well, it compiles, but won't find a smaller number if its after the bigger number)
>>and when i get rid of the array sort thing, it won't work.

I did tell you that ;-)
I'll have a think about the best way of doing this
Avatar of Gesus

ASKER

right, i'll go about it using mastermind type logic:



well ok... I want a 'master' array which is the 'answer', which is filled with random values from 0 to 9

that's what you're trying to guess

then you have a counter (so you've only got a certain number of guesses) which increments every time you make a guess.

once you've typed in a guess, first of all you'll need to take a copy of the 'master' array to use in the next stage.

then loop through your guess and find out which values you've guessed right, and if they're in the right place... if i remember right, a white peg means you guessed it right, and a black one means it's right, but in the wrong space...

when it finds a digit it gives you a coloured peg to show you that you're right and then sets that digit in the copy of the master array to -1 or something (this is to stop you typing in 8888 when the answer is 1238)...

...and that's about it really!


the problem is using java to do this !
Well, you'll have to be absolutely precise about this spec or it's impossible to design. For instance, these two statements appear to be contradictory:

>>a white peg means you guessed it right

and

>>when it finds a digit it gives you a coloured peg to show you that you're right

Maybe you'd better find out the rules or abandon them and precisely specify your own.


Avatar of Gesus

ASKER

>>a white peg means you guessed it right

and

>>when it finds a digit it gives you a coloured peg to show you that you're right

well, it gives the black or white pegs, depending on if its in the right place or not
(thats what thee first statement refers to)

when it gives you either of these colours, it also changes the corresponding number in the answer ur trying to guess.  otherwise, you could enter 8888 when trying to guess 1238 and it would say u have 1 in right place, and 3 in wrong place.
OK. So it's black or white? And four guesses allowed?
Avatar of Gesus

ASKER

say 6 attempts.

e.g

hidden (answer) = 3456

guess1 - 1234  (result 2 black pegs 3 and 4 in wrong pace)
guess2 - 2345  (result 3 black pegs 3, 4 and 5 in wrong pace)
guess3 - 3485 (2 white as 3 and 4 in right place, one black - 5)
guess4 - 3465 (2 white, 2 black)
guess5 - 3456 (4 white, end of game)
What happens when nothing is correct in an individual guess?
Avatar of Gesus

ASKER

guess1 - 1289 guess again" !
Avatar of Gesus

ASKER

public class NumberGuess {

public static void main (String[] args) {

  int guess;
  int MAX = 10;
  int[] master = new int[4];
  Random rand = new Random();
  for (int i = 0;i < 4; i++) {
   master[i] = rand.nextInt(MAX);
  }
 program.start(master);
}

 void start(int[] master) throws IOException
...
...



whats wrong here?  how do i pass an array to another method?
>>program.start(master);

can be

>>start(master);

as long as

>>void start(int[] master) throws IOException

is

>>static void start(int[] master) throws IOException


Avatar of Gesus

ASKER

start (master);
^
unreported exception java.io.IOException; must be caught or declared to be thrown
Avatar of Gesus

ASKER

sorry, all i'd missed was a

throws IOException
I'll give you a clue in which direction to head next:

write a method that returns an array of int given a user-entered String, that can be compared with the array of randoms ints
Avatar of Gesus

ASKER

can i use:

int stringinput[] = line.toIntArray();

in the way that

char stringinput[] = line.toCharArray();

is allowed?
Avatar of Gesus

ASKER

done that now!
but now i have this:

      while (line.equals("?"))
      {
      ...
      ...


how do i get it to do something if the input ISN'T "?"
while (!(line.equals("?")))

not that a question mark is a particularly good end of intput marker. What about 'X' for eXit?
Avatar of Gesus

ASKER

thanks for all the help.  i've done it now

the question mark is the input as in "i don't know the answer.  what is it?"

:D
Well i don't know the context. I assume you're reading for input until an end of input comes along, which can be whatever you define. If that's *not* the context, let me know and i'll try to advise.
Math.random () generates a random number between 0 and 1. You can multiply it by some 'n' to give a floating poinnt number between 0 and n and then use the Math.round () method to round it to the nearest integer.

int array[] = new int[4] ;

for ( int i = 0 ; i < 4 ; i ++ )
  array[i] = Math.round ( (float) ( Math.random () * n ) ) ; // end for

Here, n is the upper limit on the random numbers.

Mayank.
Avatar of Gesus

ASKER

thanks a lot everyone who helped, Cehj especially
OK ;-)