Results 1 to 6 of 6
  1. #1
    karaalis is offline Member
    Join Date
    Oct 2015
    Posts
    3
    Rep Power
    0

    Default random taking same values twice

    Hello, so it's my course work and the last thing i'm trying to polish before doing presentation about it... is -
    I did try to make - random array without duplicates... no success.
    Could anyone help me how to make so when it randoms it checks for duplicates?


    Java Code:
    package javaapplication10;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class tiklutests{
     
    private final void run(String []args){
    int ind = 0;
    int summ = 0;
    // User Name SUrname enter
    String[] sv = new String[1];                           //masīvs kurā tiks saglabāts studenta vārds uzvārds
    Scanner sc = new Scanner(System.in);                   //scanner metode
    System.out.println("Ievadiet Vārdu Uzvārdu zemāk:");   //lietotāja informācija
    for(int j=0; j<1; j++)                                 //ievietos cilvēka vārdu uzvārdu ,masīvā.
        sv[j] = sc.nextLine();                             //nolasa cilvēka vārdu uzvārdu, kas tiek ievadīts masīvā
    System.out.println("-------------------------------------------------------------------------------------");
    System.out.println("Studenta vārds uzvārds: ");       //lietotāja informācija
    System.out.println(Arrays.toString(sv));              //nolasa masīvu
    System.out.println("-------------------------------------------------------------------------------------");
    
    
             String[] questions; // jautājuma masīvs
        questions = new String[20];
        questions[0] = "Kāds datu pārraides vides vadības princips izmantots Ethernet tīklā?";
        questions[1] = "Kādā datortīklu topoloģijā izmanto terminātoru?";
        questions[2] = "Ko apraksta IEEE 802.3 standarts?";
        questions[3] = "Kādu datu pārraides maksimālo ātrumu spēj nodrošināt 5. kategorijas UTP kabelis?";
        questions[4] = "Kāda ir vārtejas (gateway) funkcionālā nozīme?";
        questions[5] = "Kāds ir tīkla kartes apzīmējums angļu valodā?";
        questions[6] = "Kāds ir datortīkla centrmezgls apzīmējums angļu valodā?";
        questions[7] = "Kāds ir neekranētā vītā pāra kabeļa apzīmējums angļu valodā?";
        questions[8] = "Ko nosaka parametrs 10 apzīmējumā 10Base5?";
        questions[9] = "Kāds kabelis tiek izmantots tīkla specifikācijā 10BaseT?";
        questions[10] = "Kura nav datortīklu topoloģija?";
        questions[11] = "Kāds komponents nav vajadzīgs lokālā tīkla izveidošanai uz vīta pāra pamata?";
        questions[12] = "Kāds apzīmējums ir vītā pāra kabeļa savienotājam?";
        questions[13] = "Kādu utilītprogrammu var izmantot izveidoto tīkla savienojumu aplūkošanai?";
        questions[14] = "Kādu interfeisu neizmanto tīkla adaptera ievietošanai datorā?";
        questions[15] = "Kā apzīmē Windows XP lokālā tīkla pamatprotokolu?";
        questions[16] = "Kas katrai tīkla kartei ir unikāls?";
        questions[17] = "Ko nodrošina optiskais kabelis salīdzinājumā ar 5.kategorijas UTP vītā pāra kabeli?";
        questions[18] = "Kā tiek apzīmēti bezvadu tīkla standarti?";
        questions[19] = "Kā notiek datu pārraide ar pusdupleksās (half-duplex) metodes palīdzību?";
    
        
            String[][] atbildes={  //atbilžu masīvs
       {" (1) centralizētā vadība (2) sacensību princips (3) ar marķiera palīdzību (4) token passing "},
       {" (1) maģistrāles tīkls (bus network) (2) zvaigžņtīkls (star network) (3) gredzentīkls (ring network) (4) tiešais kabeļa savienojums (PC-PC) "},
       {" (1) loģiskā posma vadības protokolu (2) marķiermaģistrāles tīklus (Token Ring) (3) šķiedru optikas tīklus (4) Ethernet tipa tīklus "},
       {" (1) 10 Mbit/sec. (2) 100 Mbit/sec (3) 1000 Mbit/sec (4) 16 Mbit/sec "},
       {" (1) nodrošina datu pārraides maršruta izvēli tīklos, kuriem var būt atšķirīga arhitektūra un protokoli (2) divu datu pārraides tīklu apvienošanai, ja šiem tīkliem ir dažādi protokoli (3) datu pārraides tīkla komunikācijas ierīce, kas modificē pārraidāmos signālus un ļauj pieslēgt datoru tīklam papildu darbstacijas (4) nodrošina datu pārraidi starp datoru tīkliem, kuriem ir atšķirīga arhitektūra un/vai protokoli "},
       {" (1) NIC (2) MAC (3) Hub (4) Switch "},
       {" (1) Hub (2) Switch (3) Router (4) UPS"},
       {" (1) UTP (2) STP (3) FTP4 (4) FDDI "},
       {" (1) ātrumu (2) garumu (3) pretestību (4) portu skaitu"},
       {" (1) vītais pāris (2) optiskās šķiedras (3) tievais koaksiālais (4) resnais koaksiālais"},
       {" (1) līnijas (2) zvaigznes (3) gredzena (4) maģistrāles"},
       {" (1) UTP kabelis (2) koncentrātors (3) savienotājs(4) modems "},
       {" (1) BNC (2) COM (3) RJ-45 (4) RJ-11"},
       {" (1) ping exe (2) netstat exe (3) tracert exe (4) whatsup exe "},
       {" (1) ISA (2) PCI (3) EISA (4) LPT  "},
       {" (1) NetBEUI (2) TCP/IP (3) ARCnet (4) IPX/SPX "},
       {" (1) MAC adrese (2) IP adrese (3) lietotāja vārds un parole (4) datu pārraides ātrums "},
       {" (1) lielāku datu drošību, drošumu, pārraides ātrumu (2) samazinātas tīkla izveides izmaksas (3) ērtāku un vienkāršāku izveidotā tīkla uzturēšanu, pārvaldību (4) lielāku datu drošību, bet mazāku datu pārraides ātrumu "},
       {" (1) ISO 9660, TCO 99 (2) 802.11a, 801.11b, 802.11g (3) 10Base5, 10Base2, 10BaseT (4) IEEE 802.3, IEEE 802.5  "},
       {" (1) datus var sūtīt un saņemt, bet ne vienlaicīgi (2) datus var tikai sūtīt (3) datus var tikai saņemt (4) datus var sūtīt un saņemt vienlaicīgi "},
       };
            
            String[] corr; // pareizās atbildes masīvs.
                corr = new String [21];
             corr[0] = "2";
             corr[1] = "1";
             corr[2] = "4";
             corr[3] = "2";
             corr[4] = "4";
             corr[5] = "1";
             corr[6] = "1";
             corr[7] = "1";
             corr[8] = "1";
             corr[9] = "1";
             corr[10] = "1";
             corr[11] = "4";
             corr[12] = "3";
             corr[13] = "2";
             corr[14] = "4";
             corr[15] = "2";
             corr[16] = "1";
             corr[17] = "1";
             corr[18] = "2";
             corr[19] = "1";
             
             // Testa sākuma izvade
             System.out.println("Tīklu tests nr3.");   //testa nosaukums
             System.out.println("-------------------------------------------------------------------------------------");
             System.out.println("Atbildot uz jautājumiem - lietojam atbilžu numurus 1,2,3,4.");  //lietotāja instrukcija
             System.out.println("Lietojot burutus vai simbolus , atbildes netiks ieskaitītas.");
             System.out.println("Ievadot atbildi uz jautājumu , jānospiež enter."); 
             System.out.println("*Jautājumi var atkārtoties*"); 
             System.out.println("-------------------------------------------------------------------------------------");
             
             String input2; // String mainīgais priekš lietotāja ievades(scanner).
             Scanner textscan2 = new Scanner(System.in); // scanner metode
              System.out.println("Vai esat gatavs sākt? Ievadiet J vai N"); //lietotāja instrukcija
                 input2 = textscan2.nextLine();                             //lietotājs ievada savu izvēli
                  if (input2.equalsIgnoreCase("J")){                        //ja lietotājs ievada j vai J programa iet uz nākošo soli.
                   System.out.println("-------------------------------------------------------------------------------------");
                  }
                  else{ 
                	  System.exit(0);                                       //ja lietotājs ievada jebkuru citu skaitli/burtu/simbolu ,programa beidz strādāt.
                  } 
             
             //Randomizējam jeb izvēlamies noteiktu skaitu no jautājumiem.
     Random randomize = new Random();
            //no 20 jautājumiem tiks izvēlēti 5 jauktā secībā(randomā).
            for (int i = 0; i <= 5 ; i++) {
            //iegūstam indeksu random jautajumam.
                ind= 0 + randomize.nextInt(questions.length);
            //izvada pirmo no 5 random jautājumiem.
     System.out.println(questions[ind]);
          //izvada jautājuma atbildes, balstoties uz jautājuma indeksu.
     System.out.println(Arrays.toString(atbildes[ind]));
      
      
      String input;                                   // String mainīgais priekš lietotāja ievades(scanner).
      Scanner textscan = new Scanner(System.in);      // scanner metode
       System.out.println("Ievadi atbildi zemāk: ");  // lietotāja informācija
          input = textscan.nextLine();                // nolasa lietotāja ievadi
           if (input.equalsIgnoreCase(corr[ind])){
            summ++;
            System.out.println("-------------------------------------------------------------------------------------");
           }
           else{ 
        	   System.out.println("-------------------------------------------------------------------------------------");
        }
        }
          
     System.out.println("Jūs esat atbildējis pareizi uz  " + summ +" jautājumiem.");
     System.out.println("Tests ir beidzies.");
    }
    
    public static void main(String[] args) {
       tiklutests register = new tiklutests();
       register.run(args);
    }
    }

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: random taking same values twice

    help me how to make so when it randoms it checks for duplicates?
    Can you write a small, simple code that works on generating unique numbers without all the other things that are in the posted code that makes for confusion?
    I suggest you make a method that is called and returns a unique random number.
    What is the range of the numbers to be returned and how many numbers are needed.

    i'm trying to polish before ...
    The formatting of the code needs to be fixed. Logically nested statements need to be indented. Unformatted code is harder to read and understand.
    Last edited by Norm; 11-02-2015 at 03:25 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    karaalis is offline Member
    Join Date
    Oct 2015
    Posts
    3
    Rep Power
    0

    Default Re: random taking same values twice

    Java Code:
    ind = 0;
    String[] questions;
            questions = new String[5];
            questions[0] = "2+2 = ";
            questions[1] = "3+1 = ";
            questions[2] = "4-1 = ";
            questions[3] = "4-2 = ";
            questions[4] = "2+0 = ";
    String[] ans;
            ans = new String[5];
            ans[0] = "answers";
            ans[1] = "answers";
            ans[2] = "answers";
            ans[3] = "answers";
            ans[4] = "answers";
    
         //random
     Random randomize = new Random();
            // from 5 questions it randomizes 3 with duplicates
            for (int i = 0; i <= 3 ; i++) {
            // Here we get and index for question
                ind= 0 + randomize.nextInt(questions.length);
            //Outputs question[index]
     System.out.println(questions[ind]);
          //Takes the same question index and Outputs ans array ( these are answers so user can see choices )
     System.out.println(Arrays.toString(ans[ind]));
    What do you mean by "The formatting of the code needs to be fixed. Logically nested statements need to be indented. Unformatted code is harder to read and understand. "
    Could you give me a sample how to do it, it's my first semester on java... so...

    Thanks again.
    Last edited by karaalis; 11-02-2015 at 03:56 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: random taking same values twice

    First you can create arrays like this:

    Java Code:
    String [] foo = {"1","2","3"};
    int [] nums = {1,2,3,4,5,6,7,8,9};
    And align your braces in methods and statement blocks:

    Java Code:
    if (a == 10) {
       // do something
       for (int j = 0; j < 10; j++) {
             // repeat something
       }
    }

    And why are you creating a two dimensional array for your strings? Looks like you could just use a single dimension array.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    karaalis is offline Member
    Join Date
    Oct 2015
    Posts
    3
    Rep Power
    0

    Default Re: random taking same values twice

    1 more question.
    Java Code:
     
    String[] sv = new String[1];                           //masīvs kurā tiks saglabāts studenta vārds uzvārds
    Scanner sc = new Scanner(System.in);                   //scanner metode
    System.out.println("Ievadiet Vārdu Uzvārdu zemāk:");   //lietotāja informācija
    for(int j=0; j<1; j++)                                 //ievietos cilvēka vārdu uzvārdu ,masīvā.
        sv[j] = sc.nextLine();                             //nolasa cilvēka vārdu uzvārdu, kas tiek ievadīts masīvā
    System.out.println("-------------------------------------------------------------------------------------");
    System.out.println("Studenta vārds uzvārds: ");       //lietotāja informācija
    System.out.println(Arrays.toString(sv));              //nolasa masīvu
    System.out.println("-------------------------------------------------------------------------------------");
    how can i put this in a method , so when i run my test it also runs with it.
    This code asks user to enter his name and surname and saves it in array.
    Basically i need to get at least 3 methods in my whole code...
    Last edited by karaalis; 11-02-2015 at 04:42 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: random taking same values twice

    how can i put this in a method
    Look at the tutorial on how to write a method: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    when i run my test it also runs with it.
    Look at the tutorial on how to call a method: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    There are also other good sections in the tutorial. Follow the links to see them.

    Your code already has two methods in it: run() and main(). Look at how they were defined and used.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Updating limits for random int values?
    By Cellie in forum New To Java
    Replies: 3
    Last Post: 05-04-2014, 04:13 PM
  2. Filling array with 10 random values
    By quinnvanorder in forum New To Java
    Replies: 6
    Last Post: 05-26-2012, 09:03 PM
  3. Taking specific values form Array
    By Eleeist in forum New To Java
    Replies: 17
    Last Post: 02-06-2012, 09:10 PM
  4. Replies: 22
    Last Post: 02-05-2012, 10:46 PM
  5. Replies: 0
    Last Post: 02-28-2009, 11:16 AM

Posting Permissions

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