2

I have a game of guessing words something like hangman.

This is the code I use to select a word from the words list:

List<string> words = GetWordsList();
int index = new Random().Next(words.Count);
string random = words[index];

Is there a better way to get a random item from the words list?

EDIT 1:

by better I mean (better performance or better randomness or other improvements to consider)

EDIT 2:

I call these lines every 15-30 seconds based on how much time it took to the player to guess the word.

EDIT 3:

I don't know if it is useful, but to have more information about the context, I remove the item from the list after these lines of code.

8
  • 2
    How this is not better? IMO this is clean way of doing it. Commented May 7, 2014 at 10:50
  • If you want to do that a lot you can encaspulate it in a generic extension method. Commented May 7, 2014 at 10:52
  • @all I am not sure if it is the best in performance and randomness. That's why I'm asking Commented May 7, 2014 at 10:53
  • 7
    You can keep the Random object and not create it with every call. If you call this function at very shorts intervals it might be that the time dependent value used for seeding the generator (when it's created) is the same. The result is that it produces the same numbers. So you should try to avoid that. Commented May 7, 2014 at 10:53
  • +1 for @Marius. Just an addition - with 15-30 seconds the seeding should be sufficiently distinct and random. You can continue with the solution stated above. Commented May 7, 2014 at 10:57

1 Answer 1

5

I would look at doing something like this:

var rnd = new Random();
var words = new Stack<string>(GetWordsList().OrderBy(w => rnd.Next()));

Then you just .Pop() the next value from the stack to get the next random word.

Just be mindful to instantiate the Random instance only once in your app to avoid producing non-random values related to calling your code in quick succession.

Sign up to request clarification or add additional context in comments.

2 Comments

Does it work? Is the expression in the OrderBy clause only evaluated once per item? If not, it could have some strange side effects, for instance in performance.
@StefanSteinegger - Yes, the OrderBy is only evaluated once per list element. Besides, even if it didn't the side effect would be randomizing the list anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.