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 2 of 2

Thread: Random Number problem

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random Number problem

    This is my code:

        public background()
        {    
            super(8, 8, 60);
            setBackground("cell.jpg");
            maakland();
            maakdijk();
            maakWater();
        }
        public void maakland()
        {
            land[] land = new land[16];
            int b = 0;
     
            for (int y = 6; y < 8;){
     
                for (int x = 0; x < 8;){
     
                    land[b] = new land();
                    addObject(land[b], x, y);
                    ++x;
                    ++b;
                }
                ++y;
            }
        }
        public void maakdijk()
        {
            dijk[] dijk = new dijk[24];
            int b = 0;
     
            for (int y = 3; y < 6;){
                for (int x = 0; x < 8;){
     
                    dijk[b] = new dijk();
                    addObject(dijk[b], x, y);
                    ++x;
                    ++b;
                }
                ++y;
            }
        }
        public void maakWater()
        {
            Water[] Water = new Water[32];
            int b = 0;
     
            for (int y = 0; y < 3;){
                for (int x = 0; x < 8;){
     
                    Water[b] = new Water();
                    addObject(Water[b], x, y);
                    ++x;
                    ++b;
                }
                ++y;
            }
        }
        public void randomWater(int howMany)
        {
            for(int i=0; i<howMany; i++) {
               Water water = new Water();
                int x = Greenfoot.getRandomNumber(8);
                int y = Greenfoot.getRandomNumber(8);
            }
        }
    }

    As my backgrond i have the first 3 lines: Water second 3 lines: Dike (that needs to turn to water after playing the animation). third 2 lines: Land.

    As you can see i made the background but like i said i want the dike (dijk) to turn into water using randomnumber.. or getoneobjectatoffset..
    Any idea how i can do this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random Number problem

    i want the dike (dijk) to turn into water using randomnumber
    If there are n lines that are dike, you want to randomly select one of those lines and change it to water.
    Assign the lines a number from 0 to n-1 and use the Random number generator to randomly generate a number in that range. See the API doc of the Random class for methods that will do that.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Random number
    By Imran Ahmad in forum Java Theory & Questions
    Replies: 1
    Last Post: April 30th, 2012, 11:53 AM
  2. 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
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 09:50 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