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,040 points
49 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,150 points
0 votes

You can use java.util.Random class.

int myNumber = new Random().nextInt(11) + 10
answered Aug 3, 2018 by Sushmita
• 6,840 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,040 points
301 views
0 votes
1 answer

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

I tried doing it this way - java.lang.reflect.Method ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,280 points
42 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 18, 2018 in Java by sharth
• 3,320 points
27 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,040 points
16 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,040 points
38 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,840 points
544 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 parth295
• 4,600 points
189 views
0 votes
1 answer

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

Use java.time classes instead of java.util.Date & java.sql.Date with JDBC 4.2 or ...READ MORE

answered Jun 29, 2018 in Java by Sushmita
• 6,840 points
63 views
0 votes
2 answers

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

You can convert a number into String ...READ MORE

answered Aug 10, 2018 in Java by Sushmita
• 6,840 points
436 views
0 votes
1 answer

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

I think it is most useful as ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,150 points
68 views