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;
}
Re: My code is not extracting unique number from list
Please use CODE TAGS
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)
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.
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.
Re: My code is not extracting unique number from list
Quote:
Originally Posted by
wilnicm
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) )
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..
Re: My code is not extracting unique number from list
Quote:
Originally Posted by
wilnicm
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.
Re: My code is not extracting unique number from list
Thanks for the advice..(thumbs down..) You didn' t even read the post
Re: My code is not extracting unique number from list
Quote:
Originally Posted by
wilnicm
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'.
Re: My code is not extracting unique number from list
But, it just STOPS at that line...
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;
}
Re: My code is not extracting unique number from list
Quote:
Originally Posted by
wilnicm
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.