Results 1 to 5 of 5
  1. #1
    vexity is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default showing results in a for loop randomly

    Hello there, I would like to display results from a from loop randomly and I have no idea how to do it. This is the for loop that i have:

    Java Code:
    for (int i = 0; i < object.length(); i++)
    		{
    			System.out.println(displaytext(i));
    		}
    I basically want to display some text I have stored in variables else where, but i don't want to display them serially, i want to show them randomly. I also don't want any of the text to be displayed more then once.
    All help is appreciated, thanks!
    Last edited by vexity; 04-28-2008 at 12:33 PM. Reason: forgot to mention something

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Because of your "display once" requirement you need to implement a more complicated solution. You have to implement a shuffle algorithm. For this you can randomly shuffle an array of indexes to your objects and then display the text at each shuffled index. To shuffle the array of indexes you can go once through the array with a for loop and for each index swap the value with another randomly picked location in the array. Sorry I don't have time to write the code right now. Maybe later if you still need it.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    vexity is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    I'd appreciate it much if you can still show me the code.
    -thanks

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, try this code.

    Java Code:
    import java.util.Arrays;
    import java.util.Random;
    
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class vexity {
        
        private String[] numbers = {"One", "Two", "Three",
                                  "Four", "Five", "Six", "Seven"};
        private Random random = new Random();
        
        public static void main(String[] args) {
            new vexity().doSelection();
        }
        
        private void doSelection() {
            int[] index = new int[numbers.length];
            Arrays.fill(index, -1);
            
            for (int i = 0; i < index.length; i++) {
                int num = getNumber(index);
                index[i] = num;
                System.out.println("random value: " + num);
                System.out.println("word : " + numbers[num]);
            }
        }
    
        private int getNumber(int[] array) {
            int n;
            do {
                n = random.nextInt(numbers.length);
            } while(isContain(array, n));
            return n;        
        }
        
        private boolean isContain(int[] array, int n) {
            for(int i = 0; i < array.length; i++) {
                if(array[i] == n)
                    return true;
            }
            return false;        
        }
    }
    What you have tried up to now pal. :)

Similar Threads

  1. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 03:34 AM
  2. date and calender not getting the right results
    By valoyivd in forum New To Java
    Replies: 4
    Last Post: 04-14-2008, 11:51 AM
  3. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM
  4. results to code disappear too fast for DOS window
    By dubdubdub in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •