r/learnjava • • Jan 18 '23

Learning java at 44

I've decided to learn java so I'm doing a short free course on udacity. I'm quite enjoying but I got stuck on a very basic bit of code and need some help. I'm generating random numbers for dice but now need to change the number of sides from 6 to 8 using parameters. I gone over my notes but still not quite sure where I've gone wrong. This course doesn't give me the answer anywhere so I can't see where I've gone wrong. I know (int sides) are the parameters I have to use but not sure how. Some guidance please!!

Public int roll Dice (int sides){ randomNumber=Math.random(); randomNumber=randomNumber*6; randomNumber=randomNumber+1; int randomInt=(int)randomNumber;

return randomInt; }

43 Upvotes

18 comments sorted by

View all comments

1

u/Housy5 Jan 18 '23

Is there a reason I see a lot of people use Math.random instead of the Random class?

1

u/desrtfx Jan 18 '23

People learn Math.random() earlier as it is easier to use.

Using the Random class is a bit trickier especially in the early beginning when creating objects has not been introduced.