• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Random Number

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your post is a little vague. What is it you are having trouble with?
 
Paul Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic