Welcome to Dream.In.Code
Become a Java Expert!

Join 149,613 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,847 people online right now. Registration is fast and FREE... Join Now!




Preventing duplicate numbers

 
Reply to this topicStart new topic

Preventing duplicate numbers, Random numbers no problem / duplicates are a problem

sawman
17 Sep, 2007 - 05:32 PM
Post #1

New D.I.C Head
*

Joined: 3 Feb, 2007
Posts: 21


My Contributions
Okay,

I have spent all day trying to figure this one out which I'm sure exposes my newbyness. I am able to generate 6 random numbers from within a range of 1 to 49. I can even print each set of 6 numbers 5 times. The problem is that I cannot figure out how to keep from duplicating numbers in each set of 6. it is okay if one set has a duplicate number in another set I just can't duplicate a number in the same set. Here is the code I have so far minus all the horrid attempts at not duplicating. Can anyone at least point me in the right direction?

CODE

import java.util.*;

public class Lotto
{
public static void main ( String args[] )
  {

    Random randomInt = new Random();
    final int BASE = 49;
    int ball;
    int loopCount = 1;
    while (loopCount <= 5 )
    {
    for ( int counter = 1; counter <=6; counter++ )
    {


    ball = 1 + randomInt.nextInt( 6 );

    ball = 1 + (int) (BASE * Math.random() + 1);

    System.out.printf( "%3d, ", ball );
    
    } // end for
    ++loopCount;
    System.out.println();
    } // end while

  } // end method main

  
} // end class Lotto


Thanks in advance for the help
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Preventing Duplicate Numbers
17 Sep, 2007 - 08:37 PM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
You have to store the numbers already generated (preferably in an array) and when you generate a new one, check if it is already in the array of generated numbers, and generate a new one,if it is.
Depending on the situation this solution can be optimized, but for starters it will do.
User is offlineProfile CardPM
+Quote Post

sawman
RE: Preventing Duplicate Numbers
17 Sep, 2007 - 11:30 PM
Post #3

New D.I.C Head
*

Joined: 3 Feb, 2007
Posts: 21


My Contributions
QUOTE(1lacca @ 17 Sep, 2007 - 09:37 PM) *

You have to store the numbers already generated (preferably in an array) and when you generate a new one, check if it is already in the array of generated numbers, and generate a new one,if it is.
Depending on the situation this solution can be optimized, but for starters it will do.


Okay,

Why is this particular subject such a mystery? I have spent hours researching this one little program and yet I cannot seem to figure it out. I have stored the random numbers in an array as directed. So, how do I do the check and replace part? every time I think I'm going to read a tutorial on how to do this all I get is "check the list for duplicates" I've been trying to do the homework which means reading the textbook but the textbook doesn't cover it. Please can someone be specific?

Here is the code

CODE

import java.util.*;

public class Lotto03
{
public static void main ( String args[] )
  {

    Random randomInt = new Random();
    final int BASE = 49;
    final int TOTAL = 6;
    int test[] = new int[TOTAL];
    int loopCount = 1;
    while (loopCount <= 5 )
    {
    for ( int counter = 0; counter < test.length; counter++ )
    {

    test[counter] = (int) (BASE * Math.random() + 1); // casting to integer

    System.out.printf( "%3d, ", test[counter] );
    
    } // end for
    ++loopCount;
    System.out.println();
    } // end while

  } // end method main

  
} // end class Lotto

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Preventing Duplicate Numbers
18 Sep, 2007 - 12:34 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Testing can be done in the following way:
replace
CODE

test[counter] = (int) (BASE * Math.random() + 1); // casting to integer


with

CODE

int finalIndex;
do{
  test[counter] = (int) (BASE * Math.random() + 1); // casting to integer
   finalIndex = 0;
   while(test[counter]!=test[finalIndex]){
       finalIndex++;
   }
}while(counter != finalIndex);

This simply checks if the value we've just put into the array is available at a lesser index, and if so, it will try the whole thing again.
User is offlineProfile CardPM
+Quote Post

goldenthunder
RE: Preventing Duplicate Numbers
18 Sep, 2007 - 07:42 AM
Post #5

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 13


My Contributions
or you can use a Container in which duplicate values aren't possible.
Such as Set.

Initializing the set (of the Integer type)
QUOTE
Set<Integer> testing = new TreeSet<Integer>();


then after generating your random number (the variable is named 'temp' in this example)
you check (with the contains-method), if the value is already in your Set and if it is, simply generate a new one:
CODE
Integer temp = Integer.valueOf(new Double(BASE * Math.random() + 1).intValue());
while(testing.contains(temp))
{
         temp = Integer.valueOf(new Double(BASE * Math.random() +1).intValue());
}
testing.add(temp);


whereas the output is a bit different:
CODE
++loopCount;
Iterator<Integer> iter = testing.iterator();
while(iter.hasNext())
{
    System.out.printf("%3d, ", (Integer)iter.next());
}
testing.clear();//to empty the set

The advantage of using the Set is also that you get an sorted output:
CODE
3,   6,  14,  21,  25,  26,
  1,   2,  10,  17,  20,  26,
23,  27,  30,  32,  36,  38,
  1,   3,  27,  29,  40,  49,
  3,  10,  22,  28,  35,  49,


I hope I could help you a bit.

EDIT: spellckeck wink2.gif

This post has been edited by goldenthunder: 18 Sep, 2007 - 07:43 AM
User is offlineProfile CardPM
+Quote Post

sawman
RE: Preventing Duplicate Numbers
18 Sep, 2007 - 08:26 AM
Post #6

New D.I.C Head
*

Joined: 3 Feb, 2007
Posts: 21


My Contributions
QUOTE(1lacca @ 18 Sep, 2007 - 01:34 AM) *

Testing can be done in the following way:
replace
CODE

test[counter] = (int) (BASE * Math.random() + 1); // casting to integer


with

CODE

int finalIndex;
do{
  test[counter] = (int) (BASE * Math.random() + 1); // casting to integer
   finalIndex = 0;
   while(test[counter]!=test[finalIndex]){
       finalIndex++;
   }
}while(counter != finalIndex);

This simply checks if the value we've just put into the array is available at a lesser index, and if so, it will try the whole thing again.


Thank you so much!!! Once I saw how you wrote it I was able to go back and research the syntax and now it makes perfect sense to me. Believe me I beat myself up yesterday trying to figure this one out. Yes, I'm a newbee to Java but I really try to figure it out before asking for help. This situation is kinda like learning to tie your shoes. We can do it now with our eyes closed, while chewing bubblegum, and talking at the same time. However, when we first learned how somebody showed us how. They didn't say you have to tie your shoes and sit back and laugh at our silly knots. So, I thank you for the help!

Sawman biggrin.gif

QUOTE(goldenthunder @ 18 Sep, 2007 - 08:42 AM) *

or you can use a Container in which duplicate values aren't possible.
Such as Set.

Initializing the set (of the Integer type)
QUOTE
Set<Integer> testing = new TreeSet<Integer>();


then after generating your random number (the variable is named 'temp' in this example)
you check (with the contains-method), if the value is already in your Set and if it is, simply generate a new one:
CODE
Integer temp = Integer.valueOf(new Double(BASE * Math.random() + 1).intValue());
while(testing.contains(temp))
{
         temp = Integer.valueOf(new Double(BASE * Math.random() +1).intValue());
}
testing.add(temp);


whereas the output is a bit different:
CODE
++loopCount;
Iterator<Integer> iter = testing.iterator();
while(iter.hasNext())
{
    System.out.printf("%3d, ", (Integer)iter.next());
}
testing.clear();//to empty the set

The advantage of using the Set is also that you get an sorted output:
CODE
3,   6,  14,  21,  25,  26,
  1,   2,  10,  17,  20,  26,
23,  27,  30,  32,  36,  38,
  1,   3,  27,  29,  40,  49,
  3,  10,  22,  28,  35,  49,


I hope I could help you a bit.

EDIT: spellckeck wink2.gif



Thanks for the help! I can now add this to my toolbox of java knowledge.

You guys are great! Really!!!

Sawman
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:24AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month