random 'doubles'

Discussion in 'Java' started by Jeremy Watts, Jan 26, 2006.

1. Jeremy WattsGuest

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

i can seem to produce random integers but not doubles

thanks

Jeremy Watts, Jan 26, 2006

2. Daniel DyerGuest

Daniel Dyer, Jan 26, 2006

3. Oliver WongGuest

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

>
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#random()
>
> or
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html#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

Oliver Wong, Jan 26, 2006
4. Tony MorrisGuest

> 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

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

Tony Morris, Jan 26, 2006
5. Roedy GreenGuest

On Thu, 26 Jan 2006 17:45:29 GMT, "Jeremy Watts"
<> 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.

Roedy Green, Jan 26, 2006
6. Luc The PerverseGuest

"Tony Morris" <> wrote in message
news:...
>> 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

Of course - this is integer division.

You need to cast to some type of floating point.

--
LTP

Luc The Perverse, Jan 26, 2006
7. Stefan RamGuest

"Luc The Perverse" <> writes:
>> // 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
}}

Stefan Ram, Jan 27, 2006
8. Tony MorrisGuest

"Stefan Ram" <-berlin.de> wrote in message
news:-berlin.de...
> "Luc The Perverse" <> writes:
> >> // 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
> }}
>

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/

Tony Morris, Jan 27, 2006
9. Luc The PerverseGuest

"Stefan Ram" <-berlin.de> wrote in message
news:-berlin.de...
> "Luc The Perverse" <> writes:
>>> // 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
> }}
>

Not if it's printing out 1 instead of 1.75

--
LTP

Luc The Perverse, Jan 27, 2006
10. Noodles JeffersonGuest

In article <ZU7Cf.65538\$>, 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.

Noodles Jefferson, Jan 27, 2006