i want a program that can add the generated random numbers(unique not repeating) to 2 arrays alternatively…
this is what i have done so far:
int n = 56,m=0;
int e=0,f=0;
int[] deck1 = new int[n];
int[] deck2 = new int[m];
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for(int i=1;i<=53;i++)
{
while(true)
{
next = rng.nextInt(53) + 1;
if (!generated.contains(next))
{
generated.add(next);
break;
}
}
for(int t = 2;t<=i;t++)
if(t%2==0)
{deck1[e]=next;e++;}
else
{deck2[f]=next;f++;}
}
but i am not getting the result. please help.
thanks.
You can fix your immediate problem by forgetting about the inner
forloop (iterating overt) and instead compute usingi:The problem is that currently at each iteration of the loop, you add the element to each array many times (up to i times).
Some other suggestions:
Set<Integer>(HashSet<Integer>), it is much better suited to this taskList<Integer>containing the numbers 1 through 53 and shuffle (e.g. withCollections.shuffle()). Then you could just add the first half of the list todeck1and the second half todeck2.