checkNonDuplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    checkNonDuplicates

    if we randomly generate Complex numbers how can we check if these numbers are duplicate or not??
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by mia023
    if we randomly generate Complex numbers how can we check if these numbers are duplicate or not??
    By comparing them in some way, or generating them in a fashion that precludes duplication.

    What are you trying to do? What is your goal?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by mia023
      if we randomly generate Complex numbers how can we check if these numbers are duplicate or not??
      I think when you are generating Random number then Random numbers API ensures that no duplicate numbers will be generated.

      Debasis Jana

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        if no duplicates occur, then it's not "random" anymore.
        if you want to have a sequence of numbers that "look random", but without duplicates, then you can use the System.getCurre ntTimeMillis() function. (always wait for a random number of milliseconds before getting the next number in sequence). Then you can cut off the highest digits and shuffle them if needed.

        Otherwise, to sort out the duplicates in an array that already contains random generated numbers, you have to compare the last number in array against all the others and delete if it already exists, then the second last against all the others and so on. (like bubble sort).

        or you use a Binary Tree for better performance: put the first in, then the second if the second is not already inside, then the third if it is not already inside and so on.

        Comment

        • mia023
          New Member
          • May 2007
          • 89

          #5
          Here is the way that I thought of it:

          public static boolean check_NonDuplic ate(Complex c[]){
          c=new Complex[c.length];
          for(int i=0;i<c.length; i++){
          if((c[0].getReal()!=c[i].getReal()||c[0].getImag()!=c[i].getImag()))
          return false;

          }
          return true;
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            I think when you are generating Random number then Random numbers API ensures that no duplicate numbers will be generated.

            Debasis Jana
            *ahem* did you actually *read* the API documentation of the Random class?

            kind regards,

            Jos

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by JosAH
              *ahem* did you actually *read* the API documentation of the Random class?

              kind regards,

              Jos
              Sorry :-(

              Debasis Jana

              Comment

              Working...