random 'doubles'

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

  1. Jeremy Watts

    Jeremy Watts Guest

    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
    #1
    1. Advertising

  2. Jeremy Watts

    Daniel Dyer Guest

    Daniel Dyer, Jan 26, 2006
    #2
    1. Advertising

  3. Jeremy Watts

    Oliver Wong Guest

    "Daniel Dyer" <> wrote in message
    news:eek:...
    > 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>

    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
     
    Oliver Wong, Jan 26, 2006
    #3
  4. Jeremy Watts

    Tony Morris Guest

    > 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
    #4
  5. Jeremy Watts

    Roedy Green Guest

    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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 26, 2006
    #5
  6. "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
    #6
  7. Jeremy Watts

    Stefan Ram Guest

    "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
    #7
  8. Jeremy Watts

    Tony Morris Guest

    "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
    #8
  9. "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
    #9
  10. 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
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kloofy
    Replies:
    0
    Views:
    320
    Kloofy
    Jul 8, 2005
  2. Rick
    Replies:
    3
    Views:
    1,087
    Chris Smith
    Oct 8, 2003
  3. dan
    Replies:
    1
    Views:
    2,330
    Jack Klein
    Nov 26, 2003
  4. globalrev
    Replies:
    4
    Views:
    778
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,190
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page