Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

random 'doubles'

Thread view: 
Jeremy Watts - 26 Jan 2006 17:45 GMT
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 - 26 Jan 2006 17:56 GMT
> 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?

Dan.

Signature

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

Oliver Wong - 26 Jan 2006 20:25 GMT
>> 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

:)
Roedy Green - 26 Jan 2006 23:35 GMT
On Thu, 26 Jan 2006 17:45:29 GMT, "Jeremy Watts"
<jwatts1970@hotmail.com> 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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Noodles Jefferson - 27 Jan 2006 04:17 GMT
> 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.

Signature

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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.