Results 1 to 18 of 18
Thread: Picking randomly
- 05-24-2010, 12:09 PM #1
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
- 05-24-2010, 12:14 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
See the API specs for the java.util.Random class.
- 05-24-2010, 04:29 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-26-2010, 11:41 AM #4
Eranga, i do agree with you.
at the same time, AbdulAziz Bader, you may try method shuffle of Collections:
Thomas DevosJava 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)); } }
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
- 05-27-2010, 06:15 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-27-2010, 06:23 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
The following will gives you a better explanation.
The Map Interface (The Java™ Tutorials > Collections > Interfaces)
- 05-29-2010, 11:17 PM #7
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
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.
- 05-31-2010, 03:54 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
and so on... Is that clear to you?P1 -> Q1 -> Your name
-> Q2 -> Age
P2 -> I -> Full name
-> II -> Address
- 05-31-2010, 03:55 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And according to your requirement, what you want to do is, pick a package randomly and chose questions withing that package randomly, is it?
- 05-31-2010, 06:38 AM #10
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
sorry but the logic thing is not clear... could you help me more please
- 05-31-2010, 09:09 AM #11
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
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
- 05-31-2010, 09:13 AM #12
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
the "jbutton3" is just for testing
- 05-31-2010, 05:33 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-31-2010, 05:34 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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"};
- 05-31-2010, 06:07 PM #15
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
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
- 05-31-2010, 06:13 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-31-2010, 07:00 PM #17
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
i have replaced the arraylist with random chosing
what do you think about it ?, is it better than using the arraylist ?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());
- 06-02-2010, 12:39 PM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
randomly moving shapes
By marlon in forum New To JavaReplies: 3Last Post: 10-05-2012, 09:37 AM -
RMI Server Randomly Terminating
By danberg in forum NetworkingReplies: 2Last Post: 04-26-2009, 03:36 AM -
Help! Need to randomly assign boxes
By newb101 in forum New To JavaReplies: 1Last Post: 09-16-2008, 10:57 PM -
3D Duke picking his nose and contemplating on Ruby
By Mark in forum Reviews / AdvertisingReplies: 2Last Post: 12-28-2007, 03:00 PM -
Accessing a file randomly
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks