4

I want to generate Random Float numbers between Interval [0,1]. The generated Random values will be compared with an input from a file. The content of the File is a probability (float values) and hence they are in range[0,1] and it looks like:

0.55

0.14

0.005

0.870

0.98

0

1

I am reading this File and storing probability values into a DoubleList. Now I want to generate a float Random Number between [0,1]. As you see the file have probability values upto 1 digit, 2 digits decimal and 3 digits decimal as well. I am using following Code:

public static double randomNumberGenerator()
{
    double rangeMin = 0.0f;
    double rangeMax = 1.0f;
    Random r = new Random();
    double createdRanNum = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    return(createdRanNum);
}

The random float value generated should be also be like Probabilities values (like generated upto the maximal decimal digits as in the file). How can I restrict the generated decimal digits?

I checked the following Link: Java random number between 0 and 0.06 . People suggested Int number generation and then did the double division by 100. Is this the good way? Can't we restrict the decimal generated directly?

PS: If I compare the random generated number from the above code with the double values from File, would there be some memory fall issues in the DoubleList?

Community
  • 1
  • 1
Shubham agarwal
  • 157
  • 2
  • 3
  • 14
  • You need to word your question more clearly. Am I correct that you want to generate a random number, in the range (0,x) where x is the value in the file? For example, for the first number 0.55, you want a random number in the range (0,0.55) ? – James Wierzba Oct 30 '15 at 14:07
  • `double createdRanNum = rangeMin + (rangeMax - rangeMin) * r.nextDouble();` is the best solution you will gonna get. There is no way to cheat your way out of this one without having your numbers being only semi-random, by introducing other constraints, which may follow easier logic at the expense of less true variability. – The Law Oct 30 '15 at 14:09
  • Hii James, Thanks for your answer. No. I don't want to generate the numbers in the range on the File. I want to generate the random number in the interval [0,1] (both inclusive) but I want to restrict the digits of the random number generated only to three decimal points. Presently I am getting an Output like: 0.1899367620461322 – Shubham agarwal Oct 30 '15 at 14:10
  • Hii Law, Thanks for your quick answer. I want to take only upto 3 decimal counts from the generated results by using double createdRanNum = rangeMin + (rangeMax - rangeMin) * r.nextDouble(); How do do this? – Shubham agarwal Oct 30 '15 at 14:14

1 Answers1

15

You could just use Java Random class :

Random rand = new Random();
float f = rand.nextFloat()

which returns the random float number between 0.0f (inclusive) and 1.0f(exclusive).

To round the result of the nextFloat() you could just use an helper method like the following :

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}
toto_tata
  • 14,526
  • 27
  • 108
  • 198
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • That's what I wanted to answer first, but OP seems to need to truncate decimals. – Mena Oct 30 '15 at 14:10
  • @Mena so he can truncate the result of the random next float, I think it is the correct way to go . – aleroot Oct 30 '15 at 14:12
  • Thanks a lot. Aleroot. I want to truncate the result generated from the double random. How to do that? I don"t want to round off using floor function.. – Shubham agarwal Oct 30 '15 at 14:18
  • @Shubhamagarwal I've added a round example method to my answer – aleroot Oct 30 '15 at 14:21
  • Dear Aleroot Thanks for you suggested method. I used it in generating a file but I still see that some elements still have one place after the decimal. Even though it set the paramet of decimalPlace to 3. – Shubham agarwal Oct 31 '15 at 23:17