aspose file tools*
The moose likes Beginning Java and the fly likes Random Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Number" Watch "Random Number" New topic
Author

Random Number

Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Hi,

I'm pretty new to java and trying to learn my way through. I'm looking at a part of java code ((new Random()).nextInt(4) + 1) and wanting to output a number (between 1 & 5). Only I don't really know how to do that, can anyone give me a push in the right direction?

Thanks

Paul
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Your post is a little vague. What is it you are having trouble with?


There is no emoticon for what I am feeling!
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Jeff,

I'm trying to create a random number generator for some work I've got, I've been given this code but don't really know how to implement it.

Is this any better?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Paul Jones:
...I'm looking at a part of java code ((new Random()).nextInt(4) + 1) and wanting to output a number (between 1 & 5)...

When you pass an int argument to Random's nextInt method, it generates a "random" int between 0 and the argument value. This range includes zero, but does not include the argument value.

So rand.nextInt(4) will give you 0, 1, 2, or 3. But not 4.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
So rand.nextInt(4) will give you 0, 1, 2, or 3. But not 4.

Thanks, but how would I write a method to allow me to do this?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We usually don't give complete answers here, but I think you need a jump-start to get going so here are a few usable lines. Something like:

Then you could use it like:

It seems a little clumsy to create a new MyClass just to do this job, so maybe we could change the method to static. Are you familiar with how to do that and how the caller would change? If you make that change then I didn't give you the whole answer. Give it a try and show us what you make.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Jones
Greenhorn

Joined: May 03, 2006
Posts: 11
Originally posted by Stan James:
We usually don't give complete answers here, but I think you need a jump-start to get going so here are a few usable lines. Something like:

Then you could use it like:

It seems a little clumsy to create a new MyClass just to do this job, so maybe we could change the method to static. Are you familiar with how to do that and how the caller would change? If you make that change then I didn't give you the whole answer. Give it a try and show us what you make.


Stan,

Thanks for your help, to answer your question I'm not really that familar with static methods - just started out
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As shown above, the method getNextWhatever() is an instance method, meaning you have to have an instance of the MyClass class to call the method. The static keyword makes a class method, meaning you only have to have the class to call the method. The example changes to look like:

Spot the two differences: the keyword "static" on the method makes it a class method, and the call MyClass.getNext ... doesn't require making a new instance.

How are you learning Java? Do you have a teacher or a good book to help you get going? Maybe the gang will recommend some good introductory books and web tutorials.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Stan James:




You may be setting up the OP for another lesson in Java, if his code calls that method mutliple times in quick succession -- a lesson that involves subtle differences between versions! In version 5.0, the API for Random() states:

Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.

While in versions 1.4 and before, the API for Random() states:

Creates a new random number generator. Its seed is initialized to a value based on the current time:

public Random() { this(System.currentTimeMillis()); }

Two Random objects created within the same millisecond will have the same sequence of random numbers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Number