Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: random problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default random problem

    import java.util.ArrayList;
     
    import org.riediger.jconsole.JConsole;
     
    public class Ships {
        private String name;
        private int[] location;
     
        public void setLocation(int[] array) {
            location = array;
        }
     
        public String[] createShip() {
            ArrayList<String> vertical = new ArrayList<String>();
            String[] array = { "A", "B", "C", "D", "E", "F" };
            for (String count : array) {
                vertical.add(count);
            }
     
            String[] cords = new String[2];
            cords[0] = vertical.get((int) Math.random() * vertical.size());
            cords[1] = "" + ((int) Math.random() * 6);
            return cords;
        }
     
        public static void main(String[] args) {
            JConsole.start();
            Ships one = new Ships();
            String[] array = new String[2];
            array = one.createShip();
            System.out.println(array[0]);
            System.out.println(array[1]);
     
        }
     
    }

    hello,
    this block of code gives me a strange result.
            cords[0] = vertical.get((int) Math.random() * vertical.size());
            cords[1] = "" + ((int) Math.random() * 6);
    I always get A0 as my output. What happend wrong ?
    the ouput should be B3,F2,A6 etc.


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: random problem

    Problem with casting.

    (int) Math.random() * 6
    should be
    (int) (Math.random() * 6)

    Similar correction for the other one.
    What happened in your case was that random() which is between 0.0 and 1.0 got truncated to 0.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: random problem

    arg... yes thanks =)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Random Password
    By qwerty53 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 02:46 AM
  3. random
    By b109 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 3rd, 2010, 04:19 AM
  4. Random Letter problem please help!
    By xs4rdx in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2010, 11:27 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM