Explain the functionality of Math random method

0 votes
How and where to use Math. random() method?
May 18, 2018 in Java by Daisy
• 8,140 points
1,310 views

2 answers to this question.

0 votes

 The java.lang.Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.  

int randomNumbersWithRange(int min, int max)
{
   int range = (max - min) + 1;     
   return (int)(Math.random() * range) + min;
}


Output of randomNumbersWithRange(1, 5) 5 times:

2

4

2

1

3


answered May 18, 2018 by Akrati
• 3,190 points
0 votes

You can use java.util.Random class.

int myNumber = new Random().nextInt(11) + 10
answered Aug 3, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,074 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,042 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 19, 2018 in Java by sharth
• 3,370 points
776 views
0 votes
1 answer

How to get the name of the currently executing method?

Thread.currentThread().getStackTrace() will usually contain the method you’re ...READ MORE

answered Jul 5, 2018 in Java by Daisy
• 8,140 points
823 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,140 points
1,199 views
0 votes
2 answers

How can I solve java.lang.NoClassDefFoundError in Java?

NoClassDefFoundError means that the class is present ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
34,033 views
0 votes
1 answer

What is the difference between package private, public, protected?

Modifier Class Package Subclass World Public Y Y Y Y Protected Y Y Y N No modifier Y Y N N private Y N N N   Public: Any package, any class accessibility is ...READ MORE

answered May 24, 2018 in Java by Parth
• 4,640 points
1,551 views
0 votes
2 answers

How can we convert java.util.Date to java.sql.date in Java?

It's easy to convert a java.util.Date object ...READ MORE

answered Aug 27, 2019 in Java by Sirajul
• 59,230 points
1,305 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,520 points
209,439 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,444 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP