Velocity Reviews > Java > random 'doubles'

# random 'doubles'

Jeremy Watts
Guest
Posts: n/a

 01-26-2006
how would you produce a random decimal number in java, say 0.05663?

i can seem to produce random integers but not doubles

thanks

Daniel Dyer
Guest
Posts: n/a

 01-26-2006
On Thu, 26 Jan 2006 17:45:29 -0000, Jeremy Watts <(E-Mail Removed)>
wrote:

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

http://java.sun.com/j2se/1.5.0/docs/...h.html#random()

or

http://java.sun.com/j2se/1.5.0/docs/...ml#nextDouble()

> i can seem to produce random integers but not doubles

How?

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Oliver Wong
Guest
Posts: n/a

 01-26-2006

"Daniel Dyer" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Thu, 26 Jan 2006 17:45:29 -0000, Jeremy Watts <(E-Mail Removed)>
> wrote:
>
>> how would you produce a random decimal number in java, say 0.05663?

>
> http://java.sun.com/j2se/1.5.0/docs/...h.html#random()
>
> or
>
> http://java.sun.com/j2se/1.5.0/docs/...ml#nextDouble()
>
>> i can seem to produce random integers but not doubles

>
> 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>

you should probably use it, as it may produce "better" (more random?)
results.

- Oliver

Tony Morris
Guest
Posts: n/a

 01-26-2006
> 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.2
x = 7;
y = 4;
System.out.println(x/y); // prints 1, not 1.75

--
Tony Morris
http://tmorris.net/

Roedy Green
Guest
Posts: n/a

 01-26-2006
On Thu, 26 Jan 2006 17:45:29 GMT, "Jeremy Watts"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>how would you produce a random decimal number in java, say 0.05663?
>
>i can seem to produce random integers but not doubles

see http://mindprod.com/jgloss/randomnumbers.html
--
http://mindprod.com Java custom programming, consulting and coaching.

Luc The Perverse
Guest
Posts: n/a

 01-26-2006
"Tony Morris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> 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.2
> 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.

--
LTP

Stefan Ram
Guest
Posts: n/a

 01-27-2006
"Luc The Perverse" <(E-Mail Removed)> writes:
>> // not using constant expressions (JLS 15.2
>> 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.2
x = 7;
y = 4;
System.out.println(x/y); // prints 1, not 1.75
}}

Tony Morris
Guest
Posts: n/a

 01-27-2006

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "Luc The Perverse" <(E-Mail Removed)> writes:
> >> // not using constant expressions (JLS 15.2
> >> 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.2
> 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!

--
Tony Morris
http://tmorris.net/

Luc The Perverse
Guest
Posts: n/a

 01-27-2006
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "Luc The Perverse" <(E-Mail Removed)> writes:
>>> // not using constant expressions (JLS 15.2
>>> 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.2
> 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

--
LTP

Noodles Jefferson
Guest
Posts: n/a

 01-27-2006
In article <ZU7Cf.65538\$(E-Mail Removed)>, Jeremy Watts took
the hamburger, threw it on the grill, and I said "Oh wow"...

> 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.