3

In Java, I need to randomly generate a number between 0 and 0.06.

I saw on another question on here that the following code would do the trick:

Random generator = new Random();
double number = generator.nextDouble() * .06;

However, doing that gives me really long numbers like 0.007044013589130205 and 0.03656588431980957, which I don't think is what my instructor is looking for.

Is there anyway to generate random numbers between 0 and 0.06 that only have two or three decimal places?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Brittany Gefroh
  • 81
  • 2
  • 3
  • 7

4 Answers4

9

If you want a number that ranges between [0 - 0.06] with a difference of 0.01, you can do the following:

Random generator = new Random();
int number = generator.nextInt(7);
double result = number / 100.0;

You can generalize the formula to number = generator.nextInt(max - min + 1) + min; where max and min will be the highest and the lowest number that you what to generate, respectively. This will generate a number from [min to max+1].

To increase the decimal places of the generated number one just has to divide that number by the appropriate power of 10 (e.g., 1000, 10000, ...)

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
4

Internally, Java double values are a binary floating point representation. They don't have decimal places. You can format a double as a base-10 number string having a specified number of decimal places. For example, to get three decimals (precision 0.001), you could use:

String formatted = String.format("%.3f", number);

If you don't need to do any calculations with the number and you want two or three decimal places, just print 0.0 and then a two-digit random integer between 00 and 60 (inclusive).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    "just print 0.0 and then a flat random integer between 0 and 60 (inclusive)" doesn't give you a flat distribution unless you make sure to pad to two digits. – Mike Samuel Apr 11 '13 at 05:17
  • 1
    @MikeSamuel - Oops! You're right. It's too late in the evening for me to be doing this. – Ted Hopp Apr 11 '13 at 05:23
3

I assume you need precision to be 0.001, so try this:

    Random r = new Random();
    int ret = r.nextInt(60 + 1);
    return ret / 1000.0;

this will generate [0,0.060] averagely with difference multiple of 0.001,that may be your want

BlackJoker
  • 3,099
  • 2
  • 20
  • 27
  • Don't you mean `int ret = r.nextInt(60) + 1`? – Ted Hopp Apr 11 '13 at 04:58
  • No,Random.nextInt(n) range is [0,n),in order to generate a integer in range [0,n],should use nextInt(n+1) – BlackJoker Apr 11 '13 at 05:00
  • Exactly. You are generating a random int between 0 (inclusive!) and 60 (inclusive). So the lower end of the return value range will be 0.000, not 0.001. You need `ret` to be a random int in the range [1, 60] to produce the range you claim. – Ted Hopp Apr 11 '13 at 05:02
  • the author only need 0 to 0.06 with precision 0.001 – BlackJoker Apr 11 '13 at 05:05
  • @dreamcrash - The title of OP's question says "between 0 and 0.06". Why would you want to generate 0.1? That would be an error. – Ted Hopp Apr 11 '13 at 05:06
1

Since you're dealing with sales tax, you should be aware of Why not use Double or Float to represent currency? and Using BigDecimal to work with currencies

To generate a random BigDecimal with constraints on range and # of significant digits, see How can I create a random BigDecimal in Java?

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 2
    Also worth point out that BigDecimal can be 100x slower than double to do exactly the same thing. It is also longer to write and harder to read making it more error prone, assuming you know how to round doubles. ;) – Peter Lawrey Apr 11 '13 at 07:32
  • @PeterLawrey, Yeah, few architectures don't have a dedicated FPU, but quite a few architectures still don't have hardware support for decimal so don't use it for scientific, statistical, or geometric computation. – Mike Samuel Apr 11 '13 at 16:07
  • 1
    @MikeSamuel An average android phone has FPU support. I wouldn't use hardware without an FPU for scientific, statistical or geometric computation. What sort of devices don't have FPU these days but are used for computation? – Peter Lawrey Apr 11 '13 at 16:09