473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

list question

hello, i am confused with the following code, i suppose i will get 5
numbers in the following code, but i always get 3 numbers, would u
please tell me why? thanks.
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
a.Add(5);
a.Add(6);
a.Add(7);
a.Add(8);
a.Add(9);
a.Add(10);

List<intb = new List<int>();

for(int i=0; i<5;i++){

Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber]))
{
b.Add(a[randomNumber]);
i++;
}
else
i--;
}

foreach (int i in b)
Console.WriteLine(i);

Aug 8 '06 #1
2 1107
You are adding twice to i every time you get a number (once in the loop and
once as part of the loop)

for (int i = 0; i < 5; i++) {

Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber])) {
b.Add(a[randomNumber]);
i++;
} else
i--;
}

change to ..

for (int i = 0; i < 5; i++) {

Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber])) {
b.Add(a[randomNumber]);
} else
i--;
}

or ...

int j = 0;
while (j < 5) {
Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber])) {
b.Add(a[randomNumber]);
j++;
}
}


"philip" <ma******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
hello, i am confused with the following code, i suppose i will get 5
numbers in the following code, but i always get 3 numbers, would u
please tell me why? thanks.
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
a.Add(5);
a.Add(6);
a.Add(7);
a.Add(8);
a.Add(9);
a.Add(10);

List<intb = new List<int>();

for(int i=0; i<5;i++){

Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber]))
{
b.Add(a[randomNumber]);
i++;
}
else
i--;
}

foreach (int i in b)
Console.WriteLine(i);

Aug 8 '06 #2
Golden rule, never manipulate your loop counter inside the loop beside
increase and decrease for interacting with the loop.
So, don't play with i beside in the for statement.

chanmm

"philip" <ma******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
hello, i am confused with the following code, i suppose i will get 5
numbers in the following code, but i always get 3 numbers, would u
please tell me why? thanks.
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
a.Add(4);
a.Add(5);
a.Add(6);
a.Add(7);
a.Add(8);
a.Add(9);
a.Add(10);

List<intb = new List<int>();

for(int i=0; i<5;i++){

Thread.Sleep(10);
Random randrom = new
Random(unchecked((int)DateTime.Now.Ticks));

int randomNumber = randrom.Next(0, 5);

if (!b.Contains(a[randomNumber]))
{
b.Add(a[randomNumber]);
i++;
}
else
i--;
}

foreach (int i in b)
Console.WriteLine(i);

Aug 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
How do I extract a list of lists from a user defined function and print the results as strings for each list?
0
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
1
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: saijin | last post by:
I'm planning to call a list of data from an XML file but when I duplicate the content inside the <data></data> it is not showing anything Here's the ActionScript 3.0 import...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.