Results 1 to 18 of 18
  1. #1
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Exclamation Picking randomly

    Hello... I`m creating a program that shows an questions with an mutable answers, but I won’t the questions shows randomly and not in the same sequence, that`s also on the answers. the problem is that I don`t know how to implement it.

    AbdulAziz Al-Khars

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    See the API specs for the java.util.Random class.

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

    Default

    Keep a collection, a map, with the question number and the question. Randomly pick the number and get the question. Solution is java.util.Random as r035198x says.

    Search on the forum, you can find lots of related.

  4. #4
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    Eranga, i do agree with you.
    at the same time, AbdulAziz Bader, you may try method shuffle of Collections:

    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    
    public class RandomQuestion {
    	public static void main(String[] args) {
    		List qs = new ArrayList();
    		qs.add("what is your name?");
    		qs.add("how old are you?");
    		qs.add("where are you?");
    		
    		Collections.shuffle(qs);
    		System.out.println(qs.get(0));
    		
    	}
    }
    Thomas Devos

    Quote Originally Posted by Eranga View Post
    Keep a collection, a map, with the question number and the question. Randomly pick the number and get the question. Solution is java.util.Random as r035198x says.

    Search on the forum, you can find lots of related.
    i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project

  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
    19

    Default

    Actually you've to keep them in a collection as value pairs, question and the question number, as a key value pair. Pick the key randomly and refer the relevant value from it.

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

  7. #7
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    First of all thanks for helping me with this code,
    In my program I'm trying to make three different packages, every package contains around 10 to 15 questions, and the code that you gave me will pick a random package for the user, and as usually I faced problems in implementing this idea, so any hints or suggestions.

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

    Default

    Okay, for the moment just forget about the coding part. Think about the logic. If I'm correct you have question packages, let we named them as P1, P2, P3, ... And in each package you have several questions. So basically the following kind of mapping is there.

    P1 -> Q1 -> Your name
    -> Q2 -> Age

    P2 -> I -> Full name
    -> II -> Address
    and so on... Is that clear to you?

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

    Default

    And according to your requirement, what you want to do is, pick a package randomly and chose questions withing that package randomly, is it?

  10. #10
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    sorry but the logic thing is not clear... could you help me more please

  11. #11
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    i got the logic but there is some problems in the implementation :confused:
    thats the code if you dont mind in helping me


    PHP Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    public class Easy extends javax.swing.JFrame {
    
        /** Creates new form Easy */
        String[] Question={"whats my name", "how old am i ", "whats my fathers name"};
        String[] A={"abdullah", "16", "bader"};
        String[] B={"AbdulAziz", "19", "fahad"};
        String[] C={"khaled", "21", "jamal"};
        String[] D={"bader", "20", "sameer"};
    
    
    
        public Easy() {
            initComponents();
        }
    
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            buttonGroup1 = new javax.swing.ButtonGroup();
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jButton2 = new javax.swing.JButton();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jButton3 = new javax.swing.JButton();
            jLabel2 = new javax.swing.JLabel();
            jButton4 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setResizable(false);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowOpened(java.awt.event.WindowEvent evt) {
                    formWindowOpened(evt);
                }
            });
    
            jButton1.setText("Back To Main Menu");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel1.setText("Are you Ready");
            jLabel1.addInputMethodListener(new java.awt.event.InputMethodListener() {
                public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                }
                public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                    jLabel1InputMethodTextChanged(evt);
                }
            });
    
            jButton2.setText("Exit");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("jButton3");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("jLabel2");
    
            jButton4.setText("Next");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(jLabel1)
                    .addContainerGap(306, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jRadioButton2)
                            .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 197, Short.MAX_VALUE)
                                .addComponent(jButton2)
                                .addGap(23, 23, 23)))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(jButton3)
                    .addGap(61, 61, 61)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                    .addComponent(jButton4)
                    .addGap(32, 32, 32))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addComponent(jLabel1)
                    .addGap(44, 44, 44)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 103, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3)
                        .addComponent(jLabel2)
                        .addComponent(jButton4))
                    .addGap(26, 26, 26)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    this.setVisible(false);
          new Difficulty().setVisible(true);
    
        
        }                                        
    
        private void jLabel1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {                                               
    
           
    
        }                                              
    
        private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    
    
          //  jRadioButton1.setText("ssss");
    
            
    
             List qs = new ArrayList();
    		qs.add(1);
                    qs.add(2);
                    qs.add(3);
    //                qs.add(4);
    //                qs.add(5);
    //                qs.add(6);
    //                qs.add(7);
    //                qs.add(8);
    //                qs.add(9);
    //                qs.add(10);
    
    		Collections.shuffle(qs);
    	jLabel1.setText("" + qs.get(0));
    
    
        }                                 
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.exit(0);    
        }                                        
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     jLabel2.setText(jRadioButton1.getText());
    
        }                                        
    
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
            jLabel1.setText(Question[0]);
            jLabel1.setText(Question[1]);
            jLabel1.setText(Question[2]);
        }                                        
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Easy().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        // End of variables declaration                   
    
    }
    Last edited by AbdulAziz Bader; 05-31-2010 at 09:12 AM. Reason: edting the text

  12. #12
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    the "jbutton3" is just for testing

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

    Default

    Quote Originally Posted by AbdulAziz Bader View Post
    sorry but the logic thing is not clear... could you help me more please
    Which part you are not clear with?

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

    Default

    Could you please explain what you are going to do here?

    Java Code:
    String[] Question={"whats my name", "how old am i ", "whats my fathers name"};
        String[] A={"abdullah", "16", "bader"};
        String[] B={"AbdulAziz", "19", "fahad"};
        String[] C={"khaled", "21", "jamal"};
        String[] D={"bader", "20", "sameer"};

  15. #15
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    thats related some how to the answers

    string A contains the first answer for every qustion
    string B contains the second answer for every qustion... etc

    for example lets say that the first qustion is " how old am i"
    the answers will be
    1-16
    2-19
    3-21
    4-20

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

    Default

    So if you are keeping them in that order, then you have to access with the same index. Something like this,

    Question[1], A[1], B[1], C[1]

    and the number 1 is the value you are picking randomly.

  17. #17
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    i have replaced the arraylist with random chosing


    PHP Code:
    import java.util.Random;
    
        Random lottery = new Random();
        int x;
    
        String[] Question={"whats my name", "how old am i ", "whats my fathers name"};
        String[] A={"abdullah", "16", "bader"};
        String[] B={"AbdulAziz", "19", "fahad"};
    
    jLabel1.setText(Question[x].toString());
    what do you think about it ?, is it better than using the arraylist ?

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

    Default

    My question is that, how did you pick the value for x? I mean you must select it withing the length of the array.

    Array list are much effective than, arrays. You've more control over that.

Similar Threads

  1. randomly moving shapes
    By marlon in forum New To Java
    Replies: 3
    Last Post: 10-05-2012, 09:37 AM
  2. RMI Server Randomly Terminating
    By danberg in forum Networking
    Replies: 2
    Last Post: 04-26-2009, 03:36 AM
  3. Help! Need to randomly assign boxes
    By newb101 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 10:57 PM
  4. 3D Duke picking his nose and contemplating on Ruby
    By Mark in forum Reviews / Advertising
    Replies: 2
    Last Post: 12-28-2007, 03:00 PM
  5. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:15 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
  •