Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > random 'doubles'

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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>

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


 
Reply With Quote
 
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/


 
Reply With Quote
 
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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
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




 
Reply With Quote
 
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
}}

 
Reply With Quote
 
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/


 
Reply With Quote
 
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




 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random "The IListSource does not contain any datasources" and more (Crashing a live site at random, twice a week or so) Lars-Erik Aabech ASP .Net 8 04-28-2005 07:52 AM
Random not really random... Maziar Aflatoun ASP .Net 4 08-05-2004 01:26 AM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments