CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    My code is not extracting unique number from list

    Hi,
    This is my code for generating unique random number into a label. I have 5 labels and i'm using RandomNumber method to generate numbers. So far i have been able to generate numbers but this method is not returning only unique numbers. Its sometimes returns duplicates. exp 22, 22. Thanks in advance for any help that can be given..

    private void button1_Click(object sender, EventArgs e)
    {
    Random Myrandom = new Random();
    List<int> myInts = new List<int>();

    label2.Text = RandomNumber(1, 36,Myrandom, myInts ).ToString();
    label3.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label4.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label5.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label6.Text = RandomNumber(1, 36,Myrandom, myInts ).ToString();
    //label7.Text = RandomNumber(1, 36, Myrandom ).ToString();

    }

    private void button2_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }

    private int RandomNumber(int min, int max, Random mynumb, List<int> LottNum)
    {
    int nRN = 0;
    for (int i = 1; i > 0; i++)
    {
    nRN = mynumb.Next(1, 36);
    if (!LottNum.Contains(nRN) == true)
    {
    LottNum.Add(i);
    return nRN;
    }

    }


    return nRN;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: My code is not extracting unique number from list

    Please use CODE TAGS
    Code:
    ' Like This
    I would add each 'number' to a collection, and check the collection for duplicates each time.

    then, just display the collection (you can sort it also)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: My code is not extracting unique number from list

    So think about your method for a second. What happens if your collection contains every random number from min to max? You loop forever. Bad code.

  4. #4
    Join Date
    Aug 2010
    Posts
    26

    Re: My code is not extracting unique number from list

    Well i know its looping forever, but how do i add to the list but at the same time check that the next generated number does not equal the generated random number that is on the list.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: My code is not extracting unique number from list

    Quote Originally Posted by wilnicm View Post
    Well i know its looping forever, but how do i add to the list but at the same time check that the next generated number does not equal the generated random number that is on the list.
    We understand what you are trying to do and we are trying to help you get there. Fix the infinite looping errors and step through your code in a debugger. Set a breakpoint and walk through the code - that way you'll be able to spot what is causing the duplicate entry. For example is the random generator flawed? Is the code that checks if the previous value is contained in the list flawed? Does the random generator have the ability to be rerun if the value it returned is already in the list?

    Btw, what are you trying to accomplish with this code?
    Code:
    if (!LottNum.Contains(nRN) == true)
    The '== true' portion of the statement provides no added value.

    Just write it like...
    Code:
    if ( !LottNum.Contains(nRN) )

  6. #6
    Join Date
    Aug 2010
    Posts
    26

    Re: My code is not extracting unique number from list

    I understand and you are correct, but I i have tried to use the debugger and but i cant seem to get it.
    I have tried putting a break point but i can't figure it out. This is what i'm getting when i'm debugging it.

    A. Its generating the random number.
    B. It doesn't seem like its adding the random numbers to the list for me to check
    to see against the list. I had tweaked the code several times and it does seem clear
    where I can change it

    Instead of writing the an if statement do you suggest i should use Case to deal with scenario.. The first number that is generates will always be unique. I just need to verify against the other following numbers to make sure that its unique..
    Last edited by wilnicm; August 10th, 2010 at 03:01 PM. Reason: clear up the paragraph

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: My code is not extracting unique number from list

    Quote Originally Posted by wilnicm View Post
    I understand and you are correct, but I i have tried to use the debugger and but i cant seem to get it.
    I have tried putting a break point but i can't figure it out. This is what i'm getting when i'm debugging it.
    It's time to learn the debugger. That way, you can debug your own stuff and not have to ask questions on a forum.

    To set a break point, put the cursor on a line you wish to debug and press. F9.

    Then press F5 to start debugging.

    For example,

    Put the cursor on the following line in the RandomNumber method:
    Code:
    for (int i = 1; i > 0; i++)
    Press F9 and you'll notice a red circle appears in the left margin. This is the breakpoint.

    Now press F5 to start debugging. The program will build, compile and start running until it hits the breakpoint.

    When it hits the breakpoint, you can use the mouse to hover over the variables and see their values. You can also use F10 to step over and F11 to step into code. F10 and F11 allows you to step around in your code one line at a time.

    That's really the basics of debugging.

    Go try it out.

  8. #8
    Join Date
    Aug 2010
    Posts
    26

    Thumbs down Re: My code is not extracting unique number from list

    Thanks for the advice..(thumbs down..) You didn' t even read the post

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: My code is not extracting unique number from list

    Quote Originally Posted by wilnicm View Post
    Thanks for the advice..(thumbs down..) You didn' t even read the post
    Yes Arjay, please; he doesn't want to learn, he just 'wants teh codez'.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: My code is not extracting unique number from list

    But, it just STOPS at that line...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Aug 2010
    Posts
    26

    Re: My code is not extracting unique number from list

    Hey everyone.. i figured it out.. Here is the code.. Thanks.. for all of the Help from the people that tried to help and for the people that made fun or acted stoopid. Booyah..



    private void button1_Click(object sender, EventArgs e)
    {
    Random Myrandom = new Random();
    List<int> myInts = new List<int>();

    label2.Text = RandomNumber(1, 36,Myrandom, myInts ).ToString();
    label3.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label4.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label5.Text = RandomNumber(1, 36, Myrandom, myInts ).ToString();
    label6.Text = RandomNumber(1, 36,Myrandom, myInts ).ToString();
    //label7.Text = RandomNumber(1, 53, Myrandom ).ToString();

    }




    private int RandomNumber(int min, int max, Random mynumb, List<int> LottNum)
    {
    int nRN = 0;
    for (int i = 1; i > 0; i++)
    {
    nRN = mynumb.Next(1, 36); // Generate Random number

    if (LottNum.Count == 0) // Check to see if ther is any numbers in list..
    {
    LottNum.Add(nRN); // if there is no number in the list add the random number to the list
    return nRN;
    }

    if (LottNum.Count != 0)
    {
    if (LottNum.Contains(nRN) == false)
    {
    LottNum.Add(nRN);
    return nRN;
    }
    }
    }

    return nRN;

    }

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: My code is not extracting unique number from list

    Quote Originally Posted by wilnicm View Post
    Hey everyone.. i figured it out.. Here is the code.. Thanks.. for all of the Help from the people that tried to help and for the people that made fun or acted stoopid. Booyah..
    Glad you got it figured out.

    Btw, no one intended to make fun of you or make you feel stupid. We only try to help here, but understand we often get posters that are only looking for the quick solution and aren't willing to listen or to try what we advise.

    It appeared to us that you didn't want to learn, so it seemed you fit into one of those categories so you caught a bit of flak.

    At any rate, understanding how to debug and step through the code is going to take you far, because you aren't going to have to post questions to a forum for help with simple code (because you are going to have the skills to step through the code and figure out the problems yourself).

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured