how to use nextInt() to generate random integers?

• July 22nd, 2011, 11:10 AM
rph
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 :)
• July 22nd, 2011, 11:48 AM
Norm
Re: how to use nextInt() to generate random integers?
Quote:

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
• July 22nd, 2011, 12:34 PM
rph
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.
Many thanks for your help :)
• July 24th, 2011, 08:49 PM
Junky
Re: how to use nextInt() to generate random integers?
Code java:

```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.
• November 17th, 2012, 09:46 AM
jaehoju
Re: how to use nextInt() to generate random integers?
why we need static?
• January 3rd, 2013, 01:32 AM
pbrockway2
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.