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

Thread: How to randomize the if loops?

  1. #1
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to randomize the if loops?

    I have a method something like this
    if(x = 1){
    statement1;
    break;
    }
    if(y = 1){
    statement2;
    break;
    }
    if(z = 1){
    statement3;
    break;
    }
    if(t = 1){
    statement4;
    break;
    }
    else{
    statementElse;
    }
    and I want it to shuffle the order of if loops everytime it is called .How can i do it?


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to randomize the if loops?

    If loops? Maybe, you confuse it with While loops? You can just make the order dependent on some integer and each time the method is called you generate a random integer with Random.

  3. #3
    Banned
    Join Date
    May 2011
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to randomize the if loops?

    No i mean ,when i call this method it should check randomly first x or first y or the other , and then continue the rest again randomly.I tried to do that but couldn't succeed and i think i should use the switch statement for this but i don't know how to implement

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: How to randomize the if loops?

    You can wrap each check into Runnable. Then you can define your method as, say, "public void randomized(Runnable[] array)". That is, all the operations are in an array. When the method is invoked, an integer from 0 to array.length is picked up, and the corresponding check operation is invoked. Then you remove invoked operation from a collection and recursively invoke the same method on the remainder array.

  5. The Following User Says Thank You to angstrem For This Useful Post:

    efluvio (May 26th, 2013)

  6. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How to randomize the if loops?

    Quote Originally Posted by efluvio View Post
    ...i think i should use the switch...
    I'm liking that idea. You could use an array of ints to determine the order in which the things are executed.


    A very simple approach might be something like:

        static void foo(int [] order) {
     
            for (int selection = 0; selection < order.length; selection++) {
                switch (order[selection]) {
                    case 0:
                        System.out.println("Executing first  thingie.");
                        // Do the first one
                        break;
     
                    case 1:
                        System.out.println("Executing second thingie.");
                        // Do the second one
                        break;
     
                    case 2:
                        System.out.println("Executing third  thingie.");
                        // Do the third one
                        break;
     
                    case 3:
                        System.out.println("Executing fourth thingie.");
                        // Do the fourth one
                        break;
     
                    case 4:
                        System.out.println("Executing fifth  thingie.");
                        // Do the fifth one
                        break;
                } // End switch.
            } // End for
     
        } // End foo;

    Start out with the array in any order you want, then, from the calling function, re-arrange the elements of the array each time before calling:
    public class Z {
     
        // Use rng.nextInt() in the randomShuffle method.
        static Random rng = new Random();
     
        static Scanner keyboard = new Scanner(System.in);
     
        public static void main(String [] args) {
     
            // Start with 0..4 in order
            int orderArray[] = {0, 1, 2, 3, 4};
     
            while (true) {
                // Re-arrange the order in which things will be
                // executed each time.
                randomShuffle(orderArray);
     
                foo(orderArray);
     
                System.out.print("\nPress 'Enter' to continue...");
                keyboard.nextLine();
                System.out.println();
            }
        } // End main
     
     
     
        static void foo(int [] order) {
     
            //
            // Code to do stuff in sequence, depending on the order.
            //
     
        } // End foo;
     
        public static void randomShuffle(int [] iarray) {
            // Uncomment the following for debugging
            //System.out.printf("Into   shuffle: %s\n", Arrays.toString(iarray));
     
            //
            // Code to shuffle the elements if iarray
            //
     
            // Uncomment the following for debugging
            //System.out.printf("Out of shuffle: %s\n", Arrays.toString(iarray));
        } // End shuffle
     
    } // End class definition.

    Some folks might prefer to put the shuffling inside foo so that the mechanism would be invisible to the calling function. That might be a better implementation, but the idea is the same.


    Random shuffle algorithms can be very simple and easy to implement in Java. For example there is some pseudo-code for the Fisher-Yates shuffle that can be efficiently implemented in something like seven or so lines of elementary Java.

    Anyhow...

    A sample run might look like:
    Executing fourth thingie.
    Executing third  thingie.
    Executing first  thingie.
    Executing fifth  thingie.
    Executing second thingie.
     
    Press 'Enter' to continue...
     
    Executing first  thingie.
    Executing fourth thingie.
    Executing second thingie.
    Executing third  thingie.
    Executing fifth  thingie.
     
    Press 'Enter' to continue...
     
    Executing fifth  thingie.
    Executing first  thingie.
    Executing third  thingie.
    Executing second thingie.
    Executing fourth thingie.
     
    Press 'Enter' to continue...
     
    Executing second thingie.
    Executing third  thingie.
    Executing first  thingie.
    Executing fourth thingie.
    Executing fifth  thingie.
     
    Press 'Enter' to continue...
     
    Executing fourth thingie.
    Executing first  thingie.
    Executing third  thingie.
    Executing second thingie.
    Executing fifth  thingie.
     
    Press 'Enter' to continue...
     
    Executing second thingie.
    Executing fourth thingie.
    Executing fifth  thingie.
    Executing first  thingie.
    Executing third  thingie.
     
    Press 'Enter' to continue...
     
    Executing second thingie.
    Executing fifth  thingie.
    Executing first  thingie.
    Executing third  thingie.
    Executing fourth thingie.
     
    Press 'Enter' to continue...
     
    Executing third  thingie.
    Executing first  thingie.
    Executing fourth thingie.
    Executing second thingie.
    Executing fifth  thingie.
     
    Press 'Enter' to continue...
     
    Executing first  thingie.
    Executing fifth ...

    Finally: I think that learning to write shuffling code is very worthwhile, but actually you can use the Collections.shuffle() method on a list that is based on an array of Integers. I think that's also worth a glimpse.



    Cheers!

    Z

Similar Threads

  1. Need Help! For loops and if/else
    By kram in forum Loops & Control Statements
    Replies: 2
    Last Post: February 11th, 2012, 06:22 PM
  2. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  3. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  4. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  5. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM