0
How to generate random number using random class?
I have tried to use random class to generate number from 0 to 50, but it still have an error which stated that random class have protected access. I don't understand what is the meaning.😅 What should i do to solve this. Is there another method that i can use to to generate some random integer ? From my code, it should display 3 random number, sum of the 3 generated number and average of them. https://code.sololearn.com/clCZ4nWUYYgt/?ref=app
5 Antworten
+ 4
Radin Misbah , use nextInt method instead of next. If you want to generate 3 different numbers add it in the loop otherwise in your code it generates one number, add it 3 times and output the average as integer. Better cast the sum to double to get the result. Look at the code 🐱
https://code.sololearn.com/c7KIsyMPcEvW/?ref=app
0
Wow, its working!!! Thank you. However, what if we want create a range for the generated numbers? For example that we want to generate number from 50 to 100. Did you know how to do it?
0
Radin Misbah , change line 17 => int randomnum = num.nextInt(max) + 50;
0
Then, the value of max still 50? Am i right?
0
Radin Misbah , yes try it in your code.