random 'doubles'

J

Jeremy Watts

how would you produce a random decimal number in java, say 0.05663?

i can seem to produce random integers but not doubles


thanks
 
O

Oliver Wong

Daniel Dyer said:

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
 
T

Tony Morris

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
 
L

Luc The Perverse

Tony Morris said:
// 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.

You need to cast to some type of floating point.
 
S

Stefan Ram

Luc The Perverse said:
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
}}
 
T

Tony Morris

Stefan Ram said:
Luc The Perverse said:
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
}}

I'm sure I typed int before those declarations.
I'm going to email my nntp provider for filtering out my int declarations!
 
L

Luc The Perverse

Stefan Ram said:
Luc The Perverse said:
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
}}

Not if it's printing out 1 instead of 1.75
 
N

Noodles Jefferson

Jeremy Watts took said:
how would you produce a random decimal number in java, say 0.05663?

i can seem to produce random integers but not doubles

Math.random() will.

StrictMath.random() will too.


--
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Icicle" (Tour Rehearsal) -- Tori Amos

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top