user1650737
user1650737

Reputation: 41

Take a random item from one list and add it to another list

So...

I have two lists. One is created by adding numbers 1-48 into it. The other one is empty at the beginning:

int max = 35;
int[] Numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                        25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
                        47, 48};
List<int> allNumbers = new List<int>();
List<int> newNumbers = new List<int>();

I use this method to fill up the allNumbers list:

for (int i = 0; i < Numbers.Length - 1; i++)
        {
            allNumbers.Add(Numbers[i]);
        }

My application is supposed to select a random item in the allNumbers list and add it to the newNumbers list:

Random r = new Random();
int now;

for (int i = 1; i < max; i++)
{
now = r.Next (1, allNumbers.Count);
allNumbers.Remove(now);
newNumbers.Add(now);    

This works (as in it doesn't crash or anything) but it doesn't remove the random number, i.e. I usually get the same number in my newNumbers list more than once (e.g. I could get 11, 4, 5, 5, 11, 27, 33, 4 etc.).

This is supposed to be a game somewhat similar to lottery so there can't be any double numbers. How would I go about to select a random list item from the allNumbers list, add it to the newNumbers list and then remove it from the allNumbers list? So that when the for loop goes through it again, the allNumbers.Count would be smaller by 1 and the number I previously selected is not in the allNumbers list any more.

Any help appreciated!

Upvotes: 2

Views: 1889

Answers (5)

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

the now variable represents an index (a position) inside the list of numbers. So you need to retrieve the number corresponding to this index, using the [] operator.

Then you can add this number to newNumbers list, and after you can remove the same number from allNumbers using the RemoveAt method, passing the index as parameter.

for (int i = 0; i < max; i++)
{
    now = r.Next(0, allNumbers.Count);

    newNumbers.Add(allNumbers[now]);
    allNumbers.RemoveAt(now);

}

If you prefer, you can also use the Remove method, passing the number to remove as parameter, and not the index.

allNumbers.Remove(allNumbers[now]);

be careful with the indexes, in C# they start from 0.

Upvotes: 0

Abbas
Abbas

Reputation: 14432

First of all the method you use to fill the allNumbers list is to much effort for something that can be done using the ToList() method on the array and assign it to allNumbers, like this:

List<int> allNumbers = Numbers.ToList();

The use the random to take the index from the list and add the value of the item at that index and insert it into the newNumbers list. Then you can use the index to remove it from the allNumbers list.

int index;

for (int i = 0; i < max; i++)
{
    index = r.Next (0, allNumbers.Count);
    newNumbers.Add(allNumbers[index]);
    allNumbers.RemoveAt(index);
}

Upvotes: 1

Nahuel Ianni
Nahuel Ianni

Reputation: 3185

The problem is that you are adding the number generated by the Random.Next method, instead of the content of the list on the random index.

replace your code with:

newNumbers.Add(allnumbers[now]);

That should do the trick.

Upvotes: 1

Mephy
Mephy

Reputation: 2986

You're randomizing an index with now = r.Next(allNumbers.Count);, then you try removing this index as a value. Change the above line (the index starts at 0!) and use the RemoveAt method instead of Remove.

Upvotes: 0

MarkO
MarkO

Reputation: 2233

That is because you are selecting a random index, removing the value at that index and then adding that index as a value to the newNumbers list.

Using this snippet should fix it:

for (int i = 0; i < max; i++)
{
   now = r.Next(0, allNumbers.Count);
   newNumbers.Add(allNumbers[now]);  
   allNumbers.RemoveAt(now);
}

Upvotes: 4

Related Questions