How to generate random number using random class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

4th Nov 2020, 3:54 PM
Radin Misbah
Radin Misbah - avatar
5 Answers
+ 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
4th Nov 2020, 4:13 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
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?
5th Nov 2020, 12:40 AM
Radin Misbah
Radin Misbah - avatar
0
Radin Misbah , change line 17 => int randomnum = num.nextInt(max) + 50;
5th Nov 2020, 1:27 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Then, the value of max still 50? Am i right?
5th Nov 2020, 2:47 PM
Radin Misbah
Radin Misbah - avatar
0
Radin Misbah , yes try it in your code.
5th Nov 2020, 3:40 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar