?
Solved

random numbers

Posted on 2003-03-03
47
Medium Priority
?
498 Views
Last Modified: 2010-05-18
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!)
0
Comment
Question by:Gesus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 23
  • 20
  • 3
  • +1
47 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 8056945
final int MAX = 10000;
int[] numbers = new int[4];
java.util.Random rand = new java.util.Random();
for (int i = 0;i < 4; i++) {
  numbers[i] = rand.nextInt(MAX);
}
0
 

Author Comment

by:Gesus
ID: 8056956
whats the final int max bit do?  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057000
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".
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Gesus
ID: 8057019
oh, so i get it.  so the max number would be 9999, since its exclusive, right?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057041
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
0
 

Author Comment

by:Gesus
ID: 8057045
oh, so i get it.  so the max number would be 9999, since its exclusive, right?
0
 

Author Comment

by:Gesus
ID: 8057049
whoops, sorry. didn't mean to repost then!
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 8057139
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.
0
 

Author Comment

by:Gesus
ID: 8057164
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
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 8057293
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057379
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();
     }
   }
 }
}
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 8057430
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057449
No, it won't
0
 

Author Comment

by:Gesus
ID: 8057489
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) ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057569
>>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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8059511
Is that all clear now Gesus?
0
 

Author Comment

by:Gesus
ID: 8060346
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.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8060403
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8060444
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.
0
 

Author Comment

by:Gesus
ID: 8061153
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
0
 

Author Comment

by:Gesus
ID: 8061160
eg, random generated number are 2 3 4 6

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

type thing
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063357
>>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?
0
 

Author Comment

by:Gesus
ID: 8064369
i want to be able to say "you guessed 2 correctly", or "you got 4" etc
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8064411
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 ;-)
0
 

Author Comment

by:Gesus
ID: 8064452
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
0
 

Author Comment

by:Gesus
ID: 8065749
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)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8065952
>>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
0
 

Author Comment

by:Gesus
ID: 8066220
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 !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8066326
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.


0
 

Author Comment

by:Gesus
ID: 8066354
>>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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8066920
OK. So it's black or white? And four guesses allowed?
0
 

Author Comment

by:Gesus
ID: 8066961
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)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8067934
What happens when nothing is correct in an individual guess?
0
 

Author Comment

by:Gesus
ID: 8068147
guess1 - 1289 guess again" !
0
 

Author Comment

by:Gesus
ID: 8071532
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071573
>>program.start(master);

can be

>>start(master);

as long as

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

is

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


0
 

Author Comment

by:Gesus
ID: 8071880
start (master);
^
unreported exception java.io.IOException; must be caught or declared to be thrown
0
 

Author Comment

by:Gesus
ID: 8072008
sorry, all i'd missed was a

throws IOException
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8072139
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
0
 

Author Comment

by:Gesus
ID: 8072239
can i use:

int stringinput[] = line.toIntArray();

in the way that

char stringinput[] = line.toCharArray();

is allowed?
0
 

Author Comment

by:Gesus
ID: 8073020
done that now!
but now i have this:

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


how do i get it to do something if the input ISN'T "?"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8074762
while (!(line.equals("?")))

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

Author Comment

by:Gesus
ID: 8076843
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079087
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8109210
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.
0
 

Author Comment

by:Gesus
ID: 8119670
thanks a lot everyone who helped, Cehj especially
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119697
OK ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question