This reply already exists on the site since 21/05/2014. Actually this question is the duplicate. As it was opened and answered, I will answer too.
Note that I didn’t change the solution (here I took the part that improved the method to be able to choose which data range you want, but doesn’t change the execution itself), which is the important thing. I just took the example of the strings. If the problem is not knowing how to add elements of array, then the question should be this.
using static System.Console;
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) WriteLine(valor);
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
WriteLine($"Soma novo sorteio: {array[0] + array[2]}"); // soma 1o. e 3o. elemento
}
}
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, int lowerItem, int upperItem) {
upperItem = upperItem > list.Count ? list.Count : upperItem;
lowerItem = lowerItem < 0 ? 0 : lowerItem;
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);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
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 existing there in the original question.
It was a duplicate because the previous one was not duplicate in my opinion, until the simplest answer was not the same, so for me it is not duplicate, were different situations.
– Leonardo
It’s a duplicate of the one you asked that is a duplicate of the other because it gives the same answer. When the answers to an already asked question do not satisfy you, you put a reward. But it didn’t satisfy you there because it produces exactly the result you expect? The simplest also solves in a way that you who are starting has no idea what this lot of stuff is, ñ is ready for use in any code. If you wanted something simpler you would have said you read there and wanted something "simpler" (although simpler is a subjective concept),then people would know what you are looking for.
– Maniero
There is nothing, for many i term shuffle can be defined in other words as exchange, change etc and not by searching the site you find, just think you should comment with the link of the other question that seems to be the same before marking as duplicate, because maybe that question didn’t solve the doubt that person has.
– Leonardo
Perhaps it is not enough. the fact is that it supposes. And the fact that it does not have the term does not mean that the question is not duplicated. Duplication does not mean having the same terms, it just means wanting the same result.
– Maniero