• 13
• 15
• 27
• 9
• 9

# Math.Random() - Java question

This topic is 2711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I have a quick question that Im assuming isnt to complex.

I am needing to generaterandom numbers from between 1 - 60, and 1- 100, and 1-20.

I was wondering if someone could possibly explain how to do so for me.

##### Share on other sites
Take a look at the java.util.Random class ;)

##### Share on other sites
I did, still having troubles :/
But I think I may have figured it out.

##### Share on other sites
Quote:
 Original post by DribbleDribbleI did, still having troubles :/But I think I may have figured it out.

Random rand = new Random();int i = rand.nextInt(60)+1; // 1 to 60int i = rand.nextInt(100)+1; // 1 to 100int i = rand.nextInt(20)+1; // 1 to 20

##### Share on other sites
Thanks much, that helped alot!
But for some odd reason, Im running into a problem, but I dont think it has to do with an error in the code.

Whats happend is that this program is supposed to calculate randomly, the grades of students (the amount the user inputs). It does that, but it only ever generates F's, maybe an occasional D.

##### Share on other sites
So the output is basically supposed to be like:

Student 1 got (random) on assignment 1
(random) on assignment 2
(random) on assignment 3
Etc

Students has passed/failed the class with (A,B,C,D, or F)

But its only generating scores high enough for Fs, and maybe an occasional D.

Any suggestions?

##### Share on other sites
Post your code and we can tell you why that is, otherwise we have to guess or try and read your mind from across the internets!

##### Share on other sites
Sorry, completely understand.

I will in about a day or so. Its part of an assignment, and its do in 5 minutes. But the reason Ill post it later is because I did once, and it got me into trouble cause another kid in my class coppied my code.

But its still something I need to learn, so I will post it sometime soon.