4 Replies - 8817 Views - Last Post: 19 December 2008 - 08:25 AM Rate Topic: -----

#1 Learn2LoveJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-December 08

6 unique random numbers

Posted 19 December 2008 - 06:25 AM

import java.util.Random;

import javax.swing.JOptionPane;

public class randomNODUP {

	public static void main(String[] args) {
		String output = "";

		int[] array1 = new int[6];

		// copy random numbers into array1
		for (int loop = 0; loop < 6; loop++) {
			array1[loop] = rand1to49();
			for (int loop2=0;loop2<6;loop++){
				if (array1[loop]==array1[loop2]){
					loop--;
				}
			}output = output + array1[loop];
		}

		JOptionPane.showMessageDialog(null, output + "\n", "Lottery",
				JOptionPane.INFORMATION_MESSAGE);

	}// end main
	public static int rand1to49() {
		int num;
		// create a random number generator
		Random numGenerator = new Random();
		// generate a random number between 1 and 49 inclusive
		num = Math.abs(numGenerator.nextInt(48)) + 1;
		return num;

	}// end random
}


I want 6 random numbers from 1 to 49, with no duplicates and then print them to the screen and store them to a 2d array(which might have values in already). For some reason it is not working! I know there are more advanced and easier ways to do this so any reply please stay on my beginner level.

Is This A Good Question/Topic? 0
  • +

Replies To: 6 unique random numbers

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: 6 unique random numbers

Posted 19 December 2008 - 06:34 AM

I'd suggest having a read through these:
http://www.cs.genese...nce/random.html
http://www.javapract...Action.do?Id=62
http://www.glenmccl.com/tip_010.htm
http://java.sun.com/...til/Random.html
and then starting your code again with a clearer idea of what you are trying to design.

Not trying to be rude but it's a bit of a mess at the moment and you might be better starting from scratch.

This post has been edited by janotte: 19 December 2008 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: 6 unique random numbers

Posted 19 December 2008 - 06:36 AM

Check my comments inside code.

 
for (int loop = 0; loop < 6; loop++) 
{
			int loop2; // you need this to be declared outside for loop as you need to use it outside following for loops scope as well.
			array1[loop] = rand1to49();
			for (loop2=loop-1;loop2>=0;loop--) // you are supposed to check with numbers generated, which are stored before
//																	  current number and not next locations! so you are supposed to check backwards.
			{
				if (array1[loop]==array1[loop2])
				{
					loop--;
					break;
				 }
			}
			if(loop2<0) // This means the generated number is unique, else for loop would have broken in between before reaching -1.
				output = output + array1[loop];
}



I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#4 Learn2LoveJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-December 08

Re: 6 unique random numbers

Posted 19 December 2008 - 06:46 AM

View PostAmitTheInfinity, on 19 Dec, 2008 - 05:36 AM, said:

Check my comments inside code.

 
for (int loop = 0; loop < 6; loop++) 
{
			int loop2; // you need this to be declared outside for loop as you need to use it outside following for loops scope as well.
			array1[loop] = rand1to49();
			for (loop2=loop-1;loop2>=0;loop--) // you are supposed to check with numbers generated, which are stored before
//																	  current number and not next locations! so you are supposed to check backwards.
			{
				if (array1[loop]==array1[loop2])
				{
					loop--;
					break;
				 }
			}
			if(loop2<0) // This means the generated number is unique, else for loop would have broken in between before reaching -1.
				output = output + array1[loop];
}



I hope this will help you. :)



Thank you very very much, I kind of see what is going on here but the only thing is it doesnt print anything out, the windows doesnt even pop up ;(
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: 6 unique random numbers

Posted 19 December 2008 - 08:25 AM

The logic is not bad, actually. It's the implementation that's messing you up. ;)

Your big error is the inner loop declaration, it has all kinds of issues. There are two more errors.

Here is your corrected code. Please read the comments.

Random numGenerator = new Random();
String output = "";

int[] array1 = new int[6];

// copy random numbers into array1
for (int loop = 0; loop < 6; loop++) {
	array1[loop] = Math.abs(numGenerator.nextInt(48)) + 1;
	// this is wrong in two ways
	// it increments loop instead of loop2 and it goes to the end
	//for (int loop2=0;loop2<6;loop++){
	// note, we don't want to check the last value
	// that's the test value
	for (int loop2=0;loop2<loop; loop2++){
		if (array1[loop]==array1[loop2]){
			loop--; // this is a little wonky, but can work
			// make sure you leave this loop immediately
			// you've messed with the value in loop
			break; 
		}
	}
	// oops, output doesn't know if we did a loop--
	// we'll make output later
	// output = output + array1[loop];
}

// load up output separate
for (int loop = 0; loop < 6; loop++) {
	// spaces, numbers make more sense with some in between
	output = output + array1[loop] + " ";
	
}

JOptionPane.showMessageDialog(null, output + "\n", "Lottery",
		JOptionPane.INFORMATION_MESSAGE);



Personally, I'd make a couple of methods to clear this up in main. The random number generator was not one of them, though, since it's essentially a one liner.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1