
Signature
Daniel Dyer
http://www.dandyer.co.uk
>> how would you produce a random decimal number in java, say 0.05663?
>
[quoted text clipped - 7 lines]
>
> How?
To the OP, if you're in some special environment where you can generate
random integers but not doubles (J2ME perhaps?), you can just divide your
integers to turn them into decimal values.
<pseudoCode>
Generate a random integer between zero and a million.
Divide that integer by a million to get a decimal value between 0 and 1.
</pseudoCode>
However, if you have access to the nextDouble() method mentioned above,
you should probably use it, as it may produce "better" (more random?)
results.
- Oliver
Tony Morris - 26 Jan 2006 21:49 GMT
> To the OP, if you're in some special environment where you can generate
> random integers but not doubles (J2ME perhaps?), you can just divide your
> integers to turn them into decimal values.
// not using constant expressions (JLS 15.28)
x = 7;
y = 4;
System.out.println(x/y); // prints 1, not 1.75

Signature
Tony Morris
http://tmorris.net/
Luc The Perverse - 26 Jan 2006 23:54 GMT
>> To the OP, if you're in some special environment where you can
> generate
[quoted text clipped - 5 lines]
> y = 4;
> System.out.println(x/y); // prints 1, not 1.75
Of course - this is integer division.
You need to cast to some type of floating point.
--
LTP
:)
Stefan Ram - 27 Jan 2006 00:16 GMT
>> // not using constant expressions (JLS 15.28)
>> x = 7;
>> y = 4;
>> System.out.println(x/y); // prints 1, not 1.75
>Of course - this is integer division.
This depends on the context these four lines are embedded in:
public class Main
{ public static void main( final java.lang.String[] args )
{ double x; double y;
// not using constant expressions (JLS 15.28)
x = 7;
y = 4;
System.out.println(x/y); // prints 1, not 1.75
}}
Tony Morris - 27 Jan 2006 00:23 GMT
> >> // not using constant expressions (JLS 15.28)
> >> x = 7;
[quoted text clipped - 12 lines]
> System.out.println(x/y); // prints 1, not 1.75
> }}
I'm sure I typed int before those declarations.
I'm going to email my nntp provider for filtering out my int declarations!

Signature
Tony Morris
http://tmorris.net/
Luc The Perverse - 27 Jan 2006 00:41 GMT
>>> // not using constant expressions (JLS 15.28)
>>> x = 7;
[quoted text clipped - 12 lines]
> System.out.println(x/y); // prints 1, not 1.75
> }}
Not if it's printing out 1 instead of 1.75
--
LTP
:)