# Thread: Using loops to get replace random numbers

1. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## 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. ## 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?

[code]
[/code]
to get highlighting and preserve formatting.

3. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

i want to replace the duplicates

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

## Re: Using loops to get replace random numbers

Speaking of duplicates, don't double post.

Regards,
Jim

5. ## 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?

6. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

before storing

7. ## 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?

8. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

im not sure thats y im here

9. ## 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.

10. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

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

11. ## 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.

12. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## 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. ## 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?

14. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

i wont put that card and go onto the next card

15. ## 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?

16. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

that will compare the numbers

17. ## Re: Using loops to get replace random numbers

Yes, and all the other steps needed to fill the array with unique numbers

18. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## Re: Using loops to get replace random numbers

but how to convert that to java

19. ## 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.

20. Member
Join Date
Dec 2015
Posts
22
Rep Power
0

## 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 Last