Page 1 of 3 123 LastLast
Results 1 to 20 of 41
  1. #1
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Using loops to get replace random numbers

    how to i get this to program to find duplicate numbers and replace them without using any other methods but just loops.

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Rander
      {
     
        public static void main(String[] args) {
         int n= 0;
         int l = 1;
         int[] fifty = new int[50];
          while (n <50){
          Random rand = new Random();
          int  randNo = rand.nextInt(50) + 1; 
          if (randNo==n) {
              break;
            }
            
             fifty[n] = randNo;
             System.out.printf("%03d", randNo);
             System.out.print(" ");    
                  if(l%5 == 0)
                   System.out.println();   
            n++;
            l++;
            }
        }
    }
    Last edited by dibs; 12-08-2015 at 07:53 PM.

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

    Default Re: Using loops to get replace random numbers

    How would you do it manually if there were a row of cards laid left to right with duplicates and you wanted to replace the duplicates?
    For example how would you determine if the first card on the left had a duplicate?

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    i want to replace the duplicates

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

    Default Re: Using loops to get replace random numbers

    Speaking of duplicates, don't double post.

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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    i want to replace the duplicates
    Can the values be checked for dups before storing in the array?
    Or must the scan for duplicates be done after the array is filled?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    before storing

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    Ok, how would you do it manually with playing cards? If you are laying them out left to right, what decisions would you make when you turned over the next card when there are some cards already laid out?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    im not sure thats y im here

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    Do you know what a deck of playing cards looks like? If not, then I'll have to think of another way to describe the task.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    yes i know what playing cards are but dont get what you mean

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    Ok, let me explain. If you are holding a deck of playing cards face down in your hand and want to lay out a line of 10 cards without any duplicate values in it, how would you do it? The values ignore the suit. A club 2 is the same as a heart 2.
    First you pull off a card any lay it face up. Then you pull off the next card. Here you need to decide if the new card is already being shown. If not, lay it down and move to the next card. Continue until 10 unique cards are shown.

    How would you do it? What comparisons and decisions would you have to make?

    When you get a list of the steps needed for that task, we'll work on converting those steps into java code for the random number problem.
    Last edited by Norm; 12-08-2015 at 09:38 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    i will compare the new card to every card that has been placed and then if it is not the same i will place that card

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    i will compare the new card to every card that has been placed
    Ok, that would be like a loop that starts at the first card and look at all the current cards that have been laid out.

    if it is not the same i will place that card
    Ok, what if it is the same?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    i wont put that card and go onto the next card

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    Ok.
    Now can you make a list of the steps for your program?
    There would be an outer loop that would go around until all the desired, unique numbers have been obtained.
    What would go inside that loop?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    that will compare the numbers

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    Yes, and all the other steps needed to fill the array with unique numbers
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    but how to convert that to java

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Using loops to get replace random numbers

    how to convert that to java
    You haven't listed the steps to be converted yet. When you list the steps, we'll go through them one at a time and help you write the code for it.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Using loops to get replace random numbers

    we will need to check each new number with all numbers in the array and if they dont match we will put it in the array

Page 1 of 3 123 LastLast

Similar Threads

  1. random numbers
    By lynkxs in forum New To Java
    Replies: 3
    Last Post: 03-07-2013, 09:50 AM
  2. I need help storing numbers in loops
    By MrPosotive in forum New To Java
    Replies: 26
    Last Post: 10-10-2012, 05:55 AM
  3. Help me with using random numbers!
    By EscSequenceAlpha in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 02:42 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Tags for this Thread

Posting Permissions

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