| View previous topic :: View next topic |
| Author |
Message |
zengrz Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 33 Location: ca
|
Posted: Tue May 19, 2009 7:07 pm Post subject: Need help with Java: generating 6 non-duplicated random num. |
|
|
Hi, the 6 numbers cannot repeat itself. I have put them in an array. What I have in mind is to test if the next random number generated matches anyone of the elements in the array. If no, I will add it; if yes, drop it. I am unsure about the syntax. Sorry I am a beginner.
| Code: |
/** This code is able to generate 6 random numbers, but they may not be
* different.
*/
import acm.program.*;
import acm.util.*;
public class Random40 extends ConsoleProgram {
private int[] random = new int[6];
private RandomGenerator rgen = RandomGenerator.getInstance();
public void run() {
generateRandom();
println(random[0] + " " + random[1] +
" " + random[2] + " " + random[3] +
" "+ random[4] + " " + random[5]);
}
private void generateRandom() {
int i = 0;
for (i = 0; i <= 5; i++) {
random[i] = rgen.nextInt(0, 41);
pause(50);
}
}
}
|
Thanks in advance!
(I am using acm library)
* Sorry forgot to say the numbers must be between 0 to 41. Thanks SoccaBallDude for noticing that. 
Last edited by zengrz on Tue May 19, 2009 8:06 pm; edited 1 time in total |
|
| Back to top |
|
 |
SoccaBallDude Master Cheater
Reputation: 1
Joined: 29 Aug 2008 Posts: 263
|
Posted: Tue May 19, 2009 7:59 pm Post subject: |
|
|
Quickly thrown together. Can definitely be improved.
Remember that the program will loop forever if you set the random number possibilities below the array size.
| Code: |
import java.util.Random;
public class rand
{
private static int[] random = new int[6];
private static Random rand = new Random();
private static int i;
public static void main(String[] args)
{
for (i=0; i<6; i++) generateRandom();
System.out.println(random[0] + " " + random[1] +
" " + random[2] + " " + random[3] +
" "+ random[4] + " " + random[5]);
}
private static void generateRandom()
{
int temp = rand.nextInt(41);
boolean check = false;
for (int c=0; c<random.length; c++)
{
if (random[c] == temp)
{
check = true;
System.out.println(random[c]+" at "+i);
generateRandom();
}
}
if (!check) random[i] = temp;
}
}
|
|
|
| Back to top |
|
 |
zengrz Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 33 Location: ca
|
Posted: Tue May 19, 2009 9:11 pm Post subject: |
|
|
| Quote: |
private static void generateRandom()
{
int temp = rand.nextInt(41);
boolean check = false;
for (int c=0; c<random.length; c++)
{
if (random[c] == temp)
{
check = true;
System.out.println(random[c]+" at "+i);
generateRandom();
}
}
if (!check) random[i] = temp;
}
|
Thanks for the quick reply. That is a very cool recursive method. The program works perfectly!
Can you replace the generateRandom(); in the method by rand.nextInt(41); ? Or there is a possibility that they will generate similar number again on the second try?
Is the "for loop" in that method checking for similar numbers from the first to the "random.length"th number every time a new random number is generated?
What does "if (!check)" mean? Is it the same if you replace it as
"if (check == true)" ?
Is there a non-recursive method to solve this? Sorry if that bothers you.
Thanks for the great work! |
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Wed May 20, 2009 1:19 am Post subject: |
|
|
| zengrz wrote: |
Thanks for the quick reply. That is a very cool recursive method. The program works perfectly!
Can you replace the generateRandom(); in the method by rand.nextInt(41); ? Or there is a possibility that they will generate similar number again on the second try?
Is the "for loop" in that method checking for similar numbers from the first to the "random.length"th number every time a new random number is generated?
What does "if (!check)" mean? Is it the same if you replace it as
"if (check == true)" ?
Is there a non-recursive method to solve this? Sorry if that bothers you.
Thanks for the great work! |
if(!check) : the ! means NOT. So it's equal to: if( check != true ) and also equal to if( check == false )
I don't know java, but a non-recursive method would be something like this i think:
| Code: |
boolean check;
for( int i = 0; i < 5; i++ ){
while( 1 ){
int newrandom = rand.nextInt(41);
check = false;
for( int j = 0; j < 5; j++ ){
if( random[j] == newrandom ){
check = true;
break;
}
}
if( check == false ){
random[i] = newrandom;
break;
}
}
} |
|
|
| Back to top |
|
 |
SoccaBallDude Master Cheater
Reputation: 1
Joined: 29 Aug 2008 Posts: 263
|
Posted: Wed May 20, 2009 2:58 pm Post subject: |
|
|
Improved it just a bit.
| Code: |
import java.util.Random;
public class rand
{
private static int[] random = new int[6];
private static Random rand = new Random();
public static void main(String[] args)
{
generateRandom(41);
System.out.println(random[0] + " " + random[1] +
" " + random[2] + " " + random[3] +
" "+ random[4] + " " + random[5]);
}
private static void generateRandom(int max)
{
for (int a=0; a<6; a++)
{
int temp = rand.nextInt(max);
boolean check = false;
for (int b=0; b<random.length; b++)
{
if (random[b] == temp)
{
check = true;
System.out.println(random[b]+" at "+a);
}
}
if (!check) random[a] = temp;
else a -= 1;
}
}
}
|
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed May 20, 2009 3:23 pm Post subject: |
|
|
Significantly Improved:
| Code: | public class RandNums {
public static boolean numberExistsInArray(int number, int[] array, int len) {
for (int i = 0; i < len; i++) {
if (array[i] == number) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Random rd = new Random();
int[] rands = new int[6];
for (int i = 0; i < rands.length; i++) {
do {
rands[i] = rd.nextInt(42);
} while (numberExistsInArray(rands[i], rands, i));
System.out.print(rands[i] + " ");
}
}
} |
_________________
|
|
| Back to top |
|
 |
zengrz Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 33 Location: ca
|
Posted: Thu May 21, 2009 10:55 pm Post subject: |
|
|
Thanks for all the great replies!
I have learn many new ways of solving this problem now. You guys are great! |
|
| Back to top |
|
 |
|