# maths

Discussion in 'Java' started by focode, Aug 29, 2008.

1. ### focodeGuest

when i divide 5/2 in java i get 2.0 how can i get the exact value
2.5 , actually i want to generate random number from 0 to 1 like .
00987 , .09654 ........ pls any one reply me soon

focode, Aug 29, 2008

2. ### kapkanGuest

focode Ð¿Ð¸ÑˆÐµÑ‚:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........ pls any one reply me soon

int five = 5;
int two = 2;
double result = five/twoint two = 2; // result = 2

double result = (double)five/two // result = 2.5

kapkan, Aug 29, 2008

3. ### Guest

On Aug 29, 12:47 am, focode <> wrote:
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........  pls any one reply me soon

import java.util.Random;

Random rng = new Random();
double randomNumber = rng.nextDouble();

-Lucas

, Aug 29, 2008
4. ### kapkanGuest

focode Ð¿Ð¸ÑˆÐµÑ‚:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........ pls any one reply me soon

int five = 5;
int two = 2;
double result = five/two // result = 2

double result = (double)five/two // result = 2.5

kapkan, Aug 29, 2008
5. ### kapkanGuest

focode Ð¿Ð¸ÑˆÐµÑ‚:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5

int five = 5;
int two = 2;
double result = five/two // result = 2

double result = (double)five/two // result = 2.5

kapkan, Aug 29, 2008
6. ### Roedy GreenGuest

On Thu, 28 Aug 2008 22:47:09 -0700 (PDT), focode
<> wrote, quoted or indirectly quoted someone
who said :

>when i divide 5/2 in java i get 2.0 how can i get the exact value
>2.5 , actually i want to generate random number from 0 to 1 like .
>00987 , .09654 ........ pls any one reply me soon

see http://mindprod.com/jgloss/floatingpoint.html
--

The Java Glossary
http://mindprod.com

Roedy Green, Aug 29, 2008
7. ### Roedy GreenGuest

>>when i divide 5/2 in java i get 2.0 how can i get the exact value
>>2.5 , actually i want to generate random number from 0 to 1 like .
>>00987 , .09654 ........ pls any one reply me soon

also see http://mindprod.com/jgloss/pseudorandom.html
--