2

For my Intro to computer science class I have to make an application to select restaurants with various capabilities, I can't figure out how to randomize the array though. Below is my code.

string[] myRestaurants = new string[9];

myRestaurants[0] = "Wendy's";
myRestaurants[1] = "Arby's";
myRestaurants[2] = "Olive Garden";
myRestaurants[3] = "The Pie";
myRestaurants[4] = "The Cheesecake Factory";
myRestaurants[5] = "Beto's";
myRestaurants[6] = "Dillinger's Saloon";
myRestaurants[7] = "Dayz Alpher";
myRestaurants[8] = "Firehouse subs";

var nextArray = myRestaurants.ToList();
Random rng = new Random();  
int n = nextArray.Count;
while (n > 1)
{
    n--;
    int k = rng.Next(n + 1);
    T value = nextArray[k];
    nextArray[k] = nextArray[n];
    nextArray[n] = value;
    //bang
}
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
user3121357
  • 97
  • 1
  • 1
  • 5
  • You can't just declare a generic variable in a method body, that's not how it works. Can you remove everything that isn't relevant? – Jeroen Vannevel Dec 20 '13 at 03:19

3 Answers3

5

You can randomize it with LINQ and the OrderBy method.. coupled with your Random class:

myRestaurants = myRestaurants
                    .OrderBy(x => myRandom.Next(myRestaurants.Length))
                    .ToArray();
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • +1. This will work pretty well, but the obsessive in me wants to point out the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) is an efficient way to produce a 'fairly' shuffled list. – TheEvilPenguin Dec 20 '13 at 03:25
  • Why would you put effort in implementing an algorithm when you can do the same with one line of code. I highly doubt restaurant order is crucial and needs top notch randomizing. – Jeroen Vannevel Dec 20 '13 at 03:27
  • @JeroenVannevel Given it's a three-line in-place algorithm which runs about as efficiently as possible and provides a better distribution, why not? In this case the LINQ method is probably fine, but it's worth noting the proper way to do it when distribution or performance matters. – TheEvilPenguin Dec 20 '13 at 03:33
  • @TheEvilPenguin If you're interested, I added an answer with the Fisher-Yates mechanism. :) – Abbas Dec 20 '13 at 11:21
1

The example below pics items randomly.

Random rand=new Random();
string[] RandomArray = myRestaurants.OrderBy(r => rand.Next()).ToArray();
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
Darey
  • 497
  • 4
  • 24
0

Here's a generic implementation using the Fisher-Yates mechanism:

public static class Extensions
{
    public static void Shuffle<T>(this IList<T> list)
    {
        var rnd = new Random();

        for (int i = list.Count - 1; i >= 0; i--)
        {
            var r = rnd.Next(i + 1);
            T value = list[r];
            list[r] = list[i];
            list[i] = value;
        }
    }
}

Usage:

myRestaurants.Shuffle();

Example output:

  • The Pie
  • Dillinger's Saloon
  • Beto's
  • Olive Garden
  • The Cheesecake Factory
  • Dayz Alpher
  • Firehouse subs
  • Arby's
  • Wendy's
Abbas
  • 14,186
  • 6
  • 41
  • 72