Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: how to use nextInt() to generate random integers?

1. ## how to use nextInt() to generate random integers?

So basically for my assignment, I need to generate a random integer number between 20 and 90. My textbook leaves a lot to be desired in the explanation of using nextInt(). I want to assign the random integer value to an int that I have already declared called randomAngle. Can anyone explain to me the correct syntax to get this?
I hope I've made myself clear, many thanks

2. ## Re: how to use nextInt() to generate random integers?

using nextInt()
What class is the nextInt() method in?
The API doc for the class and that method should explain what it does.
Link to the API doc:Java Platform SE 6

3. ## The Following User Says Thank You to Norm For This Useful Post:

rph (July 22nd, 2011)

4. ## Re: how to use nextInt() to generate random integers?

Figured it out now, all i had to do was write:

static Random randGen = new Random();
int randomAng = randGen.nextInt(70) + 20;

in the public class and make sure I put:

import java.util.Random;

at the beginning.

5. ## Re: how to use nextInt() to generate random integers?

```static Random randGen = new Random();
int randomAng = randGen.nextInt(70) + 20;```
Your code will generate random numbers from 20 to 89 inclusive. Read the API to find out why 90 will be excluded.

6. ## Re: how to use nextInt() to generate random integers?

why we need static?

7. ## Re: how to use nextInt() to generate random integers?

I think we need static to avoid what is described in Java docs:

"If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers."

Making the variable static will only help if it is reused later. The main thing is create only one instance of the Random class and use it in all subsequent calls. It is also described in more detail here http://faq.wordgraphs.com/faq/13450

8. ## Re: how to use nextInt() to generate random integers?

This is an old thread. And I'm sure you don't want to give the impression that you are using this forum to plant links to your site.

The link adds nothing beyond that which Junky has already alluded to. In particular it does not address the question of why the OP chose to make the variable static. It's a perfectly reasonable thing to do, but without knowing what his or her intentions were we can only guess (pointlessly) at the reason.