I am working for online examination project.
my work is to generate random number without repetition.
i tried and i am getting numbers to be repeated sometime....
I need to know how to check the repetition in the generated number.


random r=new random();
for(i=0;i<10;i++)
{
int j=r.nextint(10);
System.out.println(j);
}

Since you are only generating up to 10 numbers, I would create an arraylist with all the possible values then generate an index into that arraylist and retreive and remove that index, then you are definately not repeating anything and you don't even need to check.

thanks for your idea...also i need to know any unique method available for non repeated random number sequence generation.....
i am just looking for open source regarding this project...If you could help me,plz do so...thanks...

The method is the one given, the only other method is to use a while loop and continue generating numbers, comparing them to the ones already generated, until you generate one that hasn't been generated yet. Not very effecient, that. If you are simply looking for cut-n-paste code (for an obvious homework assignment) then you are on your own.

thanks...lemme try with the loop....since i am a beginner,i jus asked for reference...

yes...the arraylist idea is working sir....thanks..still my process goes in retrieving stored questions from xml file where the generated random number matches the question number..

Okay? And that means?

there are around 200 stored questions in xml file format and i need to retrieve questions randomly from the xml file.i am doubted that is it possible to retrieve only some node elements of xml file..

200 is nothing. Parse the xml file into a list, then make a copy of that list to actually random index into (deleting those accessed indexes, of course).

Actually i could parse the xml document and retrieve element value....also i could generate random no in an arraylist...now dono how to access this parsed xml data

I will give u solution , because that is one of my old problems (random and not repeated)

public static void main(String[] args) 
    {
        int RandomNumbers =100;
          ArrayList<Integer> numbers = new ArrayList<Integer>();
        int number = (int)(Math.random() * RandomNumbers);
        numbers.add(number);
        for (int i = 0; i < RandomNumbers-1; i++) {
            do{
               number = (int)(Math.random() * 100);
            }while(numbers.indexOf(number)!=-1);
           numbers.add(number);

        }
       for(Integer i: numbers){
           System.out.println(i);
       }

     
    }

I have solution , because that one of my old problems :D

public static void main(String[] args) 
    {
        int RandomNumbers =100;
          ArrayList<Integer> numbers = new ArrayList<Integer>();
        int number = (int)(Math.random() * RandomNumbers);
        numbers.add(number);
        for (int i = 0; i < RandomNumbers-1; i++) {
            do{
               number = (int)(Math.random() * 100);
            }while(numbers.indexOf(number)!=-1);
           numbers.add(number);

        }
       for(Integer i: numbers){
           System.out.println(i);
       }

     
    }

hmmm....its worthy...thanks.....

now i am with one more confusion in designing gui using swing.I have one label,radiobutton and a button('next'),when i click the button,text in the label and radio button need to change with new data.need help

class guimtd extends JFrame
{
 JButton NEXT;
 JLabel pno,problem;
 JRadioButton option1,option2,option3,option4;
 Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "db1";
    String driverName = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "mantra";
    String query2="select * from destination";
 
 
 //final JTextField  text1,text2;
 guimtd()
  {  
    final JFrame f=new JFrame("exam");
    f.setLayout(null);
    pno = new JLabel();
    problem = new JLabel();
    option1 = new JRadioButton();
    option2 = new JRadioButton();
    option3 = new JRadioButton();
    option4 = new JRadioButton();
    
    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(option1);
    bgroup.add(option2);
    bgroup.add(option3);
    bgroup.add(option4);

    JPanel radioPanel = new JPanel();
    radioPanel.setLayout(new GridLayout(3, 1));
    radioPanel.add(option1);
    radioPanel.add(option2);
    radioPanel.add(option3);
    radioPanel.add(option4);
    radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Choices"));
    
    NEXT = new JButton("NEXT");
    
    pno.setBounds(100,100,50,50);
    problem.setBounds(150,100,400,100);
    option1.setBounds(250,220,100,20);
    option2.setBounds(250,270,100,20);
    option3.setBounds(250,320,100,20);
    option4.setBounds(250,370,100,20);
    NEXT.setBounds(500,370,100,20);
    f.setVisible(true);
    f.setSize(1024,768);  
    
    NEXT.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
  {   
       int c=0;
   try
   {
      Class.forName(driverName).newInstance();
      con = DriverManager.getConnection(url+dbName, userName, password);
      Statement st = con.createStatement();
      ResultSet rs1 = st.executeQuery(query2);
      while(rs1.next())
      {
         
      if(ae.getSource()==NEXT)   
      {
       removeAll();
       c++;
       NEXT.setText("clicked " + c + " times!");
                          String d1=rs1.getString(1);
                          String d2=rs1.getString(2);
                          String d3=rs1.getString(3);
                          String d4=rs1.getString(4);
                          String d5=rs1.getString(5);
                          //String d5=rs1.getString(5);
                            pno.setText(d1);
                            problem.setText(d2);
                            option1.setText(d3);
                            option2.setText(d4);
                            option3.setText(d5); 
                            
                            
       }
       }    
                            
                             rs1.close();
                             st.close();
                             con.close();
     }
   catch(Exception e)
               {
                       System.out.println("Error"+e);
                      
               }
   }
   });
            f.add(pno);
            f.add(problem);
            
            f.add(NEXT);      
            
            f.repaint();
            f.add(option1);
            f.add(option2);
            f.add(option3);
            f.add(option4);
                 
  }
  }
class gui
{
  public static void main(String arg[])
  {
      guimtd window = new guimtd();
     window.setVisible(true);
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.