-1

Is it possible to return a value like 1.125155, which is a double value greater than 1?

My code:

Math.random()
ajb
  • 31,309
  • 3
  • 58
  • 84
vala manoj
  • 21
  • 1
  • 7
  • `random.nextDouble()` ? – Suresh Atta Mar 01 '17 at 07:05
  • can it possible it 1.0 something like in math.random? – vala manoj Mar 01 '17 at 07:08
  • 1
    You'll need to clarify exactly what you want What values do you want to return? Do you want values uniformly distributed over some range, or do you want some other distribution? – ajb Mar 01 '17 at 07:10
  • Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 – santosh gore Mar 01 '17 at 07:14
  • What do you want ; More than one values or value greater than 1?? – Mehraj Malik Mar 01 '17 at 07:14
  • @santoshgore i read out it give in between 0.0 to 1.0 but i have question it's posible 1 > – vala manoj Mar 01 '17 at 07:16
  • its is clearly mentioned that less than 1.0 . – santosh gore Mar 01 '17 at 07:19
  • You are suffering from an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (and from not knowing where to find the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--)) - state your actual problem and ask us to help you there, rather than asking about a particular solution, that is in your case impossible. – Erwin Bolwidt Mar 01 '17 at 07:22
  • @ErwinBolwidt thank you .. it's not possible in my case. – vala manoj Mar 01 '17 at 07:29
  • public static double random() { return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); } private static final class RandomNumberGeneratorHolder { static final Random randomNumberGenerator = new Random(); } using this possible ? – vala manoj Mar 01 '17 at 07:52

5 Answers5

3

This is not possible if you are using java.lang.Math. (Read Doc)

mahieus
  • 580
  • 3
  • 17
  • public static double random() { return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); } using this possible ? – vala manoj Mar 01 '17 at 07:51
  • private static final class RandomNumberGeneratorHolder { static final Random randomNumberGenerator = new Random(); } – vala manoj Mar 01 '17 at 07:51
  • @valamanoj Maybe you can explain what you are trying to achive? – mahieus Mar 01 '17 at 08:17
  • above internal implementation of random method can u tell me It is possbile to get value grater then 1 ? – vala manoj Mar 01 '17 at 08:49
  • @valamanoj no it is not possible. You can use the result to get a value between a certain range say 0-2 or 0-100 but you have to implement this yourself. – mahieus Mar 01 '17 at 11:58
1

You can do something like this :

 Random ran = new Random();
 double x = ran.nextDouble() + 1

The x is now the random number that has double value greater than 1.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
0

You can move the decimal place using BigDecimal like this...

Random ran = new Random();
double result = BigDecimal.valueOf(ran.nextDouble()).movePointRight(1).doubleValue()

If you don't have to convert back to double the code will be cleaner and you'll have more control over rounding issues if you plan on using the returned value in further calculations (above I have converted back to double as this is what your OP asked for).

Brad
  • 15,186
  • 11
  • 60
  • 74
0

Math.random() returns a double from [0,1]; Therefore, to get it to return a number great than 0 you would have to do one of a couple of things

Math.random() + 1 returns an number from [0+1, 1+1] or [1,2]

Math.random() * 2 returns a number from [0*2, 1*2] or [0,2]

-3

There is a workaround to it if you want to use two values which possibly should be random and never be same(random function sometimes give same value when executed twice), then put your values to a list and use list.shuffle() Hope this helps.

Skeptic Scribbler
  • 527
  • 1
  • 6
  • 18