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 :)
Re: how to use nextInt() to generate random integers?
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
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 :)
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.
Re: how to use nextInt() to generate random integers?
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.