## Ashenvale

``````            public int intRet1;
public int intRet2;

public void IntRandom(int intNumber)
{
int intNum[] = new int[36];
Random randomGen = new Random();

intNum[0] = randomGen.nextInt(intNumber)+1;

do{
intNum[1] = randomGen.nextInt(intNumber)+1;
}while(intNum[0] == intNum[1]);

intRet1 = intNum[0];
intRet2 = intNum[1];

}
``````

today I tried using java.util.Random class, but as you can see I have 36 arrayElement and each element is unique from the rest(note, intNumber has a corresponding value 42, 45, 49, 55; depends on which button I press) what I'm thinking is I can just write the rest of the code by repeating the do while() statement, change the indexExponent and the while statement like this:

``````          do{
intNum[1] = randomGen.nextInt(intNumber)+1;
}while(intNum[0] == intNum[1]);

do{
intNum[2] = randomGen.nextInt(intNumber)+1;
}while(intNum[0] == intNum[2] && intNum[1] == intNum[2]);
``````

But I think my code would be too long. Is there another way that I can write this shorter?

## NormR1 563

each element is unique from the rest

A couple of ways to do this.
Before saving the new number, search the previously saved numbers for matches.
If the range of numbers isn't too large, used a boolean array to remember what numbers have been saved.
Use a Set object to keep track of previously used numbers.

## Ashenvale

can you give me an example on how to use a boolean array?

## stultuske 1,092

why do you think working with a boolean array is any different than working with an array of int's? (which you are already doing).

so, either you know enough to work with one, or this is just some code you copied.

## Ashenvale

this code is something I made myself, I was just curious about what NormR1 said about using boolean array

## NormR1 563

Use the generated number to index the array.
If not set, set it and use the number.
If set, go back and get a new number.

why do you think working with a boolean array is any different than working with an array of int's? (which you are already doing).

Yeah, because nobody ever learned anything, every programmer in the world just woke up one day and understod everything.

Ashenwale: As I understand it, you want an array with values 1 through 37 ordered randomly. Correct?

## Ashenvale

``````int intNum[] = new int[36];
int tempNum;
Random randomGen = new Random();

intNum[0] = randomGen.nextInt(intNumber)+1;

for(int ctr=1; ctr<intNum.length; ctr++) {
tempNum = randomGen.nextInt(intNumber)+1;

for(int i=0; i<intNum.length; i++)
{
if(tempNum == intNum[i])

tempNum = randomGen.nextInt(intNumber)+1;

else {
intNum[ctr] = tempNum;
}
}
}
``````

there is still something wrong with my code 2 - 3 elements are having the same value.

## NormR1 563

How does the code test for previous occurrances of a number in the intNum array?

Can you post some pseudo code that describes the logic?

## stultuske 1,092

Yeah, because nobody ever learned anything, every programmer in the world just woke up one day and understod everything.

I never claimed everone just "wakes up and understands it all". but if you are using an array of int's, and you are showing you know how to work with their indices, I assume you can ... work with an array and use their indices.

never claimed everone just "wakes up and understands it all". but if you are using an array of int's, and you are showing you know how to work with their indices, I assume you can ... work with an array and use their indices.

I get where you are comming from, I just think you are jumping to a conclusion :)
You're right, he should know.
He obviously does not though, so let's se if he'll learn :)
For me the off-topic ends here I suppose.

Ashenwale: As NormR1 is saying your code never checks for duplicate numbers.
The nextInt(n) method generates a number K such that (0<=K<n), regardless of previously generated numbers. There is no guarantee that it will not return the same number all 36 times.

## Ashenvale

this is the sample output when 6/42 button is pressed.

What I need for 6 / 42 is to have 6 combination number per row. The number generated for each textField must be different but as you can see there are a lot of numbers that are the same.

:( @Bloodman242 (range is from 1-42)

## NormR1 563

The number generated for each textField must be different but as you can see there are a lot of numbers that are the same.

If you don't want duplicate numbers, you need to design some logic that will get numbers that are unique. Several methods have been suggested. Try one of them or come up with a new one.

## Taywin 312

If you are storing number up to the total you need (i.e. need to store 42 numbers which is from 1 to 42), how about creating an array of size 42 and stores number 1 to 42 in order. Then, randomly swap 2 number each time up to m times. That would reduce both time and space complexities (Time will be O(n) or O(m) where n is the number of array size and m is the time swapping. Space will be O(n)).

If you are storing number up to the total you need (i.e. need to store 42 numbers which is from 1 to 42), how about creating an array of size 42 and stores number 1 to 42 in order. Then, randomly swap 2 number each time up to n times. That would reduce both time and space complexities.

This would have been my suggestion too.
Well, that or a knuth shuffle.

Of course the difference is that Ashenvale needs i< 42 numbers between 1 and 42 inclusive, but that should be a problem.
Ashenvale: I would like to know what you think the problem is?
You need to think through the program's logic if you want it to work :)

## Ashenvale

I already thought of a way to do this. Not the best solution, but for now I think this is ok.
@Taywin :D This is the first time I've heard about swapping, I'll try to read about it.

Everyone, thank you so much for replying! :) I'll try to read books and practice more...

## Taywin 312

but that should be a problem.

I think it is a typo. I believe Bladtman242 means "but that should not be a problem" instead. :)