-1

I have a problem about my c# project. In the program I have a list

private static void FillData(List<Question> questions)
    {
        AddQuestion(questions,
            "DotA isimli MOBA oyununun açılımı nedir? ",
            2,
            "Defense of the Arkham",
            "Defence of the Ancients",
            "Defense of the Ancients",
            "Dance of the Architectures"
            );

like that and i wanna make that list randomizely showned in every opening with shuffle. In the example my teacher used

private static void Shuffle(Soru[] array)
    {
        int length = array.Length;
        for (int i = 0; i < length; i++)
        {
            int index = i + ((int) (_random.NextDouble() * (length - i)));
            Soru soru = array[index];
            array[index] = array[i];
            array[i] = soru;
        }

this and i wanna make that in Guid.NewGuid(). Can anyone help me with replacing that with Guid?


(edited from comments) This is what I tried so far:

private static void Shuffle(List<Question> List) 
{
    List = List.OrderBy(o => Guid.NewGuid().ToString()).ToList();
    foreach (Question question in ....) { Console.WriteLine(question);
}

but I can't fullfill foreach loop. And yes I want to use Guid instead of random.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • 3
    Welcome to StackOverflow! I don't fully understand what you mean by "make that in Guid.NewGuid()"... could you explain a little more what you want to achieve? Use a guid instead of random? – René Vogt Jun 01 '16 at 12:38
  • 2
    Possible duplicate of [Randomize a List](http://stackoverflow.com/questions/273313/randomize-a-listt) – Chris Pickford Jun 01 '16 at 12:39
  • The point of that algorithm is to randomly pick an index to swap with the current item. Since a GUID is not a number, you need to explain how you intend to incorporate a GUID into the algorithm ,or if you want a _different_ algorithm that can use a GUID. – D Stanley Jun 01 '16 at 12:42
  • 1
    What do you mean by _I can't fullfill forach loop_? If you use Linq you don't need a foreach loop. FYI, you don't need to use `ToString`. – juharr Jun 01 '16 at 12:48
  • @juharr You do need a foreach loop for outputting to the console like he is doing; he needs foreach loop for generating the output, not pulling together the results. – Brian Mains Jun 01 '16 at 12:49
  • As of my understanding, you won't get a randomly sorted array by using guid. The standard way GUIDs are generated via Guid.NewGuid() is based on some system parameters and the current time. All Guids should be generated so that the previous guid is smaller than the next generated GUID. The standard guid generation is optimized to use the guids as Index in a Database and only produces GUIDs in an ascending manner. see: [GUIDs are designed to be unique, not random](https://blogs.msdn.microsoft.com/oldnewthing/20120523-00/?p=7553) – BoeseB Jun 01 '16 at 12:50
  • 1
    Your List will be not sorted after method Shuffle has run. You first example replaces elements inside the array. Your second example sets the List to another objectreference. – dvjanm Jun 01 '16 at 12:51
  • 1
    @BoeseB, not sure what you mean by "standard guid generation" but Guid.NewGuid() does not create an ascending sequence when called repeatedly. (tested on .Net 4.0 and 4.5) – cdkMoose Jun 01 '16 at 12:59
  • @cdkMoose I wasn't sure if Guid.NewGuid() generates version 1 guids which are ascending or version 4 guids which produce some pseudo random guids. But my point is for true randomness you shouldn't use GUIDs. They aren't designed for randomness – BoeseB Jun 01 '16 at 13:02

1 Answers1

1

The problem is that you only modified the input parameter to hold a reference of another List. As it is not passed as reference, in the place where you have called it will hold the reference of the original List.

You could try this way:

    private static void Shuffle(List<Question> List)
    {
        var randomOrderList = List.OrderBy(o => Guid.NewGuid().ToString()).ToList();

        for (int i = 0; i < List.Count; i++)
        {
            List[i] = randomOrderList[i];
        }
    }
dvjanm
  • 2,351
  • 1
  • 28
  • 42
  • Thank you so much. It worked, but as I am new at c# I dont quite understand code that you wrote. Can you explain your code by using comment lines? – Ceren Cömert Jun 01 '16 at 13:14