aspose file tools*
The moose likes Beginning Java and the fly likes Random Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Numbers" Watch "Random Numbers" New topic
Author

Random Numbers

Richard Jones
Greenhorn

Joined: May 24, 2006
Posts: 17
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

Joined: May 24, 2006
Posts: 17
sorry put new instead of nextInt
Richard Jones
Greenhorn

Joined: May 24, 2006
Posts: 17
See method should be

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

Still will not compile confused
Can anyone help?
Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12
Did you remember to import java.util.Random;
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Also look at the parentheses.
Richard Jones
Greenhorn

Joined: May 24, 2006
Posts: 17
Yes got import java.util.Random;
but get
((new Random()).nextInt(4)+1); not a statement
Richard Jones
Greenhorn

Joined: May 24, 2006
Posts: 17
Also look at the parentheses.
--------------------

SCJP 1.4 - 90%

Sorry dont understand ?
Tomas Anderson
Greenhorn

Joined: Feb 26, 2006
Posts: 16

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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Jan 17, 2006
Posts: 1296
And as Keith pointed out, your parentheses don't match up.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Richard Jones
Greenhorn

Joined: May 24, 2006
Posts: 17
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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
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

Joined: May 24, 2006
Posts: 17
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: May 24, 2006
Posts: 17
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

    Joined: May 24, 2006
    Posts: 17
    I Have sorted the Random problem now thanks for help
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Random Numbers