Saturday, 15 January 2011

c# - Extension Method to return random alphabet -


How to develop an extension method, which is characterized by random characters from alphabet (A, B, ....) Single character)).

  public static character random letter (this four random characters) {}  

An extension method here is the wrong tool, because a random letter in the alphabet is not related to the input sheet (or am I missing something here?).

  Static String Alphabet = "QWERTYUIOPASDFGHJKLZXCVBNM"; Stable random = new random (); Public static character Random Litter () {return alphabet (random) Next (alphabet lang)]; }  

Edit (respond to comments):

You create it in an extension method on random type:

Can be: Public Static Characters Next Random Letter (This Random Random) {Return Alphabetical [Random Next (alphabet lang)]; }

(I do not want to call it nextLetterOfTheAlphabet because it should be clearly random)

Synchronizing thread-protection However, I think the whole locking process is more CPU taxing than pulling a random number:

  public static characters Synchronized ray Ndum litter () {lock (random) // Although the lock is not random;) {return alphabet (random) Next (alphabet lang)]; }}  

No comments:

Post a Comment