1

Possible Duplicate:
Access random item in list

I want to randomly generate an element from a string list, however I have no idea about how to achieve this. I have 4 elements : aaa, bbb, ccc, ddd. I want to generate one of them to draw on the screen randomly, I search some piece of code of C# but it's not working. Does anyone know how to make this?

Community
  • 1
  • 1

3 Answers3

6

Check out this link for drawing text in XNA:

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Writing_text.php

After you have that part, you simply need to create a list and select a random element to be passed to spriteBatch.DrawString(). This is a quick untested version of what that might look like. You should fix this to suit your code.

Random r = new Random();
string rand = list[r.Next(list.Count)];

spriteBatch.DrawString(spriteFont, rand, new Vector2(20, 45), Colors.Black);
Gray
  • 7,050
  • 2
  • 29
  • 52
  • @facebook-100000499165748: glad it works. I'm not so sure about the edits, but be sure to accept the answer if the question can be closed. – Gray Jan 30 '13 at 00:01
5

Sure, easy enough:

List<string> list = new List<string>() { "aaa", "bbb", "ccc", "ddd" };

int l = list.Count;

Random r = new Random();

int num = r.Next(l);

var randomStringFromList = list[num];

Also next time you should include the code that doesn't work along with (possible) reasons why.

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • I was just wondering why you choose list.Count() over just list.Count. Is there some difference I am not aware of? – Gray Jan 29 '13 at 23:55
  • 1
    I was sure the extension version is faster than the property one, but I just ran a test and turns out the property is about five times faster than the extension. So you should use the one without the parentheses. – user1306322 Jan 30 '13 at 00:07
  • I think the reason is because you do all the work up front to create a List. It keeps track of that count for you, so you might as well use it. Count() is more useful for the lower level stuff like plain old arrays. That is my understanding of it anyway. That is useful to know that for sure though, rather than just assuming. – Gray Jan 30 '13 at 00:09
  • 2
    `Count()` is useful when you don't know exactly what kind of collection you're dealing with. `ILists` and `Arrays` both know exactly how many items they hold, so calling the `Count` or `Length` properties respectively is very cheap. When you have an `IEnumerable` for instance, you don't know what kind of collection it is, so you have to use the `Count()` extension method, which iterates over the collection to count the items (very much slower). – MattDavey Jan 30 '13 at 00:11
  • @MattDavey: Thanks for the Correction on Arrays. Somehow the length property slipped my mind. IEnumerables were a much better example. – Gray Jan 30 '13 at 00:13
4

I am not sure if this is what you need, but why not create a random integer and then use string[int] to access your string array.

namespace ConsoleApplication1
{
  using System;
  using System.Text;

  class Program
  {
    static void Main(string[] args)
    {
      Random random = new Random();
      string[] myStrings = new string[] { "aaa", "bbb", "ccc", "ddd" };

      for (int n = 0; n < 10; n++)
      {
        int rnd = random.Next(0, myStrings.Length);
        string s = myStrings[rnd];
        Console.WriteLine("-> {0}", s);
      }

      Console.ReadLine();
    }
  }
}
Alina B.
  • 1,256
  • 8
  • 18