Shuffle numbers from a list? [duplicate]

2

Well, initially I do not understand much of a list in C #, but I have a list of type int that will always have 4 elements that are: 1, 2, 3 e 4 , I'm assigning them with .Add , if someone knows another Give me a little help ..

I wanted to shuffle the items on this list every time, ie have a function to shuffle the items in the list and return the scrambled list so I can use it.

  

Example:

     

A list: 1, 2, 3, 4

     

Return: 2, 1, 4, 3 (randomly)

And after that, I have to use the elements in this list, how do I do this? In the example list the scrambling became 2, 1, 4, 3 how do I use each value in the code as an integer variable? Example: I want to assign to a variable soma the value of the 1st and 3rd element of the scrambled list, how do I indicate the 1st and 3rd element for soma ?

    
asked by anonymous 10.09.2015 / 06:20

3 answers

3

Here's a simple solution:

var list = new List<int> {1, 2, 3, 4};
var rnd = new Random();

var query =
    from i in list
    let r = rnd.Next()
    orderby r
    select i;

var shuffled = query.ToList();
    
10.09.2015 / 10:13
1

This answer teaches you to do some algorithms, such as Fisher-Yates , but it is important to note that this algorithm does not is 100% random, according to the answer itself. The answer gives other more reliable solutions, but for the sake of simplicity, I will keep only the first one:

private static Random rnd = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rnd.Next(n + 1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

Usage:

List<int> numeros = new List<int> { 1, 2, 3, 4 };
numeros.Shuffle();

To add the first with the third element, you can do the following:

var soma = numeros[0] + numeros[2]; // 0 é a primeira posição, 2 é a terceira.
    
10.09.2015 / 07:45
1

This response has been on the site since 5/21/2014 . This question is actually the duplicate duplicate . As it remained open and was answered, I will respond as well.

Note that I did not change the solution (here I took the part that improved the method to choose which data range you want, but does not change the execution itself), which is the important thing. I just took the strings part from the example. If the problem is not knowing how to add elements from the array , then the question should be this.

using System;
using System.Collections.Generic;

public static class Sorteio {
    public static void Main() {
        int[] array = { 1, 2, 3, 4 };
        array.Shuffle();
        foreach (var valor in array) {
            Console.WriteLine(valor);
        }
        Console.WriteLine("Soma: {0}", array[0] + array[2]); // soma 1o. e 3o. elemento
        //vamos de novo
        array.Shuffle(); //com poucos númros tem chance de repetir
        Console.WriteLine("Soma novo sorteio: {0}", array[0] + array[2]);
    }
}

namespace System.Collections.Generic {
    public static class IListExt {
        static Random r = new Random(DateTime.Now.Millisecond);

        public static void Shuffle<T>(this IList<T> list) {
            upperItem = list.Count;
            lowerItem = 0;
            for (int i = lowerItem; i < upperItem; i++) {
                int j = r.Next(i, upperItem);
                T tmp = list[j];
                list[j] = list[i];
                list[i] = tmp;
            }
        }

        public static void Shuffle<T>(this IList<T> list, int upperItem) {
            list.Shuffle(0, upperItem);
        }

        public static void Shuffle<T>(this IList<T> list) {
            list.Shuffle(0, list.Count);
        }
    }
}

See running on dotNetFiddle .

To assign to a variable just do this:

var soma = array[0] + array[2]; //listas começam em zero então o elemento sempre é -1 ao desejado

For more details, see the answers already there in the original question.

    
10.09.2015 / 12:09