I got some problem here..I wanted to generate 6 digits number without repeating into a textbox when the form load in java...but i m stuck and lost here...someone can help me ??

Recommended Answers

All 4 Replies

Sure we can help you! Just paste whatever code you have written so far so we can attempt to help you.

Here's an algorithm that should help.
1. Put digits into an array. E.g.,
int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
2. Write some code that generates 2 random numbers between 0 & 9. Store the random numbers in 2 variables, say num1 and num2.
3. Write some code that uses num1 and num2 as indices into the digits array, and swaps the 2 digits stored at those locations. So, for example, if num1 = 4 and num2 = 2, then after the swap, digits looks like this: {0, 1, 4, 3, 2, 5, 6, 7, 8, 9}.
4. Write a loop that executes the code in steps 2 & 3 a few times. How many times? try 5 or 10.
5. Use the first 6 digits in the array to populate your textbox.

yea..bro...i got the concept..but how to generate random numbers ..using For loop ?? because I need this 6 digits to be loaded in the form registration..after the user register, user will use this 6 digits to buy ticket..So this 6 digits cannot be reapeated..

1. Use the javadoc and look up either the Random class or Math.random().
2. If each number needs to be unique, then does it really need to be random in the first place? Why not just give the first person the number 1, the next person the number 2, etc.? That would be a much easier way to give each person a unique number.
3. If you really have to give each person a unique random number, you could do this instead:
- Create a collection (not an array, maybe an ArrayList?) with 1,000,000 elements
- In a loop, fill the collection with numbers 0 to 999,999 in order
- Do the above once. Then each time you need to give out a number,
- Get the current size of the collection
- Generate a random number between 0 & size-1
- Use that number to index into the collection
- Give out the number at that index
- Remove the number at that index from the collection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.