Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Numbers

 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using the following get a message not a statement could anyone tell me why
please ?

Public void between1And5()
{
((new Random()).newInt(4)+1;
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry put new instead of nextInt
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See method should be

Public void between1And5()
{
((new Random()).nextInt(5)+1;

Still will not compile confused
Can anyone help?
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you remember to import java.util.Random;
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look at the parentheses.
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes got import java.util.Random;
but get
((new Random()).nextInt(4)+1); not a statement
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look at the parentheses.
--------------------

SCJP 1.4 - 90%

Sorry dont understand ?
 
Tomas Anderson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Public void between1And5()
{
((new Random()).nextInt(5)+1;


If that's your whole method, you've got problems. You have to close the method with a curly bracket }.

In addition, you need to look into void methods. Is a method that returns nothing really what you want?

Good luck,
TBA
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a few more spelling errors. Write public, not Public. And don't create a new Random object every time you use the method; if you create two Random objects within 0.1 seconds of each other, you will get the same number returned from them both.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as Keith pointed out, your parentheses don't match up.
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random RandomNumberGenerator = new Random();
int randomNumber = (RandomNumberGenerator.nextInt(5)+1);

Still trying to get a return of 5 could anyone help Please
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Richard Jones:
Random RandomNumberGenerator = new Random();
int randomNumber = (RandomNumberGenerator.nextInt(5)+1);

Still trying to get a return of 5 could anyone help Please


From the code, randomNumber should be a number between 1 and 5. What do you mean by "get a return of 5"? Are you saying that the result is not a random number between 1 and 5?

Henry
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard-
Your most recent two lines of code work fine for me. Just put them in a method.
Also, if this is supposed to be random, why are you expecting 5?
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numbers just need to be between 1 and 5 but when I type random I get numbers in hex

How do I call the number which I have created between 1 and 5 ?
 
Paul Clapham
Sheriff
Posts: 21314
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "get" here, exactly what do you mean? Because the nextInt() method just returns an int value. That's just a number. It isn't "in hex" or "in decimal" or "in binary" or anything. Those descriptions only apply when you choose to display that number in text form to the user (in this case you). So you must be asking about some code where you display the number. I haven't seen any such code yet. So could you post it? Preferably post a small executable piece of code with an explanation of what you expect to see and what you actually see.
 
Richard Jones
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping that the following code would make an object move between 1 and 5 places but instead get Semantic error: Message randomLeft() not understood by class'Frog'


/* instance methods */

/**
* Decrements the position of the receiver by a number between 1 and 5.
*/
public void randomLeft()
{
this.setPosition(this.getPosition() + randomNumber);


}
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a strange sounding message - "Message *** not understood".

Let me suggest the following. The best possible way to get help in this forum is to post the shortest possible complete example program that clearly demonstates your error. It should only be a few lines long! Most of your previous posts have been vague and confusing, so some clarity from you would be welcome.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been following the progress of your posts and would suggest:-
  • Write out a RandomGenerator class with 4 methods in
  • A constructor which creates an instance of the java.util.Random class. Like your randomNumberGenerator on the example you posted. But make sure randomNumberGenerator is a field.
  • A getRandomInt() method which returns an int.
  • A print() method which prints out the random number you have generated.
  • A main method with something like "new RandomGenerator().print();"
  • You don't need anything else.

    Leave off the moving ball for the time being. Compile the class and run it in the usual fashion until you get a print out of the random number. Then you can see for yourself on screen what sort of numbers you are getting. When you have got this RandomGenerator class working, you can go back to the moving ball.

    You have already posted the text for your constructor (except that "RandomNumberGenerator" oughtn't to have a capital R).
    You have half the text of the getRandomInt() method in the same posting.
    In the print method write something like
    CR
    [ May 31, 2006: Message edited by: Campbell Ritchie ]
     
    Richard Jones
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I Have sorted the Random problem now thanks for help
     
    Jeff Albertson
    Ranch Hand
    Posts: 1780
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Richard Jones:
    I Have sorted the Random problem now thanks for help


    Jolly good. But remember: try to post code that forum members can copy, paste, compile and run.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic