Random number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christo

    #1

    Random number

    hello I am new to java very new and am hoping someone can offer me some
    help.

    I know that i need to use java.util.Rando m somewhere to generate a random
    number

    can anyone show me the code to do this very basic code to simply output the
    random number that has been generated to the console?

    I would really appreciate this, I can work from there to expand on what i
    need ti to do

    TIA

    Christo

  • Mark Rafn

    #2
    Re: Random number

    Christo <chris.me.white @hotmail.comwro te:
    >can anyone show me the code to do this very basic code to simply output the
    >random number that has been generated to the console?
    public class RandomCoin {
    /** flip a coin. */
    public static void main(String[] args) {
    java.util.Rando m rand = new java.util.Rando m();
    System.out.prin tln(rand.nextBo olean() ? "heads" : "tails");
    }
    }

    Of course, if you prefer some other random type than boolean, call the
    appropriate method - see the javadoc.

    If you're using it for cryptography, you might prefer SecureRandom.
    --
    Mark Rafn dagon@dagon.net <http://www.dagon.net/>

    Comment

    • j1mb0jay

      #3
      Re: Random number

      This will create you a random double.

      //Creates random double.
      double random = Math.random();

      //Turns double into a number >=1
      double Random = random * 10;

      //Cast the double as an int (removes decimals)
      int xOry = (int)Random;

      --
      Regards JJ (UWA)

      "Christo" <chris.me.white @hotmail.comwro te in message
      news:L6ydnbRgzP DCilTYnZ2dnUVZ8 vednZ2d@bt.com. ..
      hello I am new to java very new and am hoping someone can offer me some
      help.
      >
      I know that i need to use java.util.Rando m somewhere to generate a random
      number
      >
      can anyone show me the code to do this very basic code to simply output
      the random number that has been generated to the console?
      >
      I would really appreciate this, I can work from there to expand on what i
      need ti to do
      >
      TIA
      >
      Christo

      Comment

      • Ian Shef

        #4
        Re: Random number

        "j1mb0jay" <jap6@aber.ac.u kwrote in news:1170886729 .974948@leri.ab er.ac.uk:
        This will create you a random double.
        >
        //Creates random double.
        double random = Math.random();
        >
        //Turns double into a number >=1
        No, because random may be a number less than 0.1
        For example, 0.04382 * 10 becomes 0.4382

        Perhaps you meant that this expands the range of the random number?
        double Random = random * 10;
        >
        //Cast the double as an int (removes decimals)
        ....by truncation, may not be what you want. Consider Math.round().
        int xOry = (int)Random;
        >
        There is some good information on generation of pseudorandom numbers in Java
        at






        --
        Ian Shef 805/F6 * These are my personal opinions
        Raytheon Company * and not those of my employer.
        PO Box 11337 *
        Tucson, AZ 85734-1337 *

        Comment

        • j1mb0jay

          #5
          Re: Random number

          Sorry have only started messing with java when I started this course at
          university, otherwise use to C#, was just trying to help, but if didn't know
          it could return 0.0xxx I must edit code to allow for this mistake. Thanks
          for the comment.

          --
          Regards JJ (UWA)

          "Ian Shef" <invalid@avoidi ng.spamwrote in message
          news:Xns98D17CA FD9762vaj4088ia nshef@138.126.2 54.210...
          "j1mb0jay" <jap6@aber.ac.u kwrote in
          news:1170886729 .974948@leri.ab er.ac.uk:
          >
          >This will create you a random double.
          >>
          >//Creates random double.
          >double random = Math.random();
          >>
          >//Turns double into a number >=1
          >
          No, because random may be a number less than 0.1
          For example, 0.04382 * 10 becomes 0.4382
          >
          Perhaps you meant that this expands the range of the random number?
          >
          >double Random = random * 10;
          >>
          >//Cast the double as an int (removes decimals)
          >
          ...by truncation, may not be what you want. Consider Math.round().
          >
          >int xOry = (int)Random;
          >>
          >
          There is some good information on generation of pseudorandom numbers in
          Java
          at

          >
          >
          >
          >
          >
          --
          Ian Shef 805/F6 * These are my personal opinions
          Raytheon Company * and not those of my employer.
          PO Box 11337 *
          Tucson, AZ 85734-1337 *

          Comment

          • Rune Zedeler

            #6
            Re: Random number

            Ian Shef wrote:
            ...by truncation, may not be what you want.
            Usually truncation is what you want. Round is normally never what you want.
            For instance to throw a dice, use 1+(int)(Math.ra ndom()*6).

            Math.round(Math .random()*10) gives 0 with 5% probability, 10 with 5%
            probability and 1-9 with 10% probability each.

            -Rune

            Comment

            Working...