fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7. namespace Util
  8. {
  9. public class RandomWordMatch
  10. {
  11. private readonly string baseChars = "1234567890";
  12. private readonly string defaultMatchWord = "114514";
  13.  
  14.  
  15.  
  16. private bool _ProgressDisplay = true;
  17.  
  18. public bool ProgressDisplay { get { return _ProgressDisplay; } set { _ProgressDisplay = value; } }
  19.  
  20. public RandomWordMatch()
  21. {
  22. }
  23.  
  24. public RandomWordMatch(string baseChars, string defaultMatchWord = "114514")
  25. {
  26. if (string.IsNullOrWhiteSpace(baseChars))
  27. throw new ArgumentNullException("baseChars");
  28. else if (string.IsNullOrWhiteSpace(defaultMatchWord))
  29. throw new ArgumentNullException("defaultMatchWord");
  30.  
  31. this.baseChars = baseChars;
  32. this.defaultMatchWord = defaultMatchWord;
  33. }
  34.  
  35.  
  36. public void Start(string matchWord = null, int limit = int.MaxValue)
  37. {
  38. if (string.IsNullOrWhiteSpace(matchWord))
  39. matchWord = defaultMatchWord;
  40.  
  41. var wordQueue = new Queue<char>();
  42.  
  43. foreach (var c in GetRandomString(matchWord.Count()))
  44. wordQueue.Enqueue(c);
  45.  
  46. int count = 0;
  47. while (count < limit)
  48. {
  49. if (wordQueue.SequenceEqual(matchWord))
  50. {
  51. foreach (var c in wordQueue)
  52. Console.Write(c);
  53.  
  54. Console.WriteLine(
  55. "\n\n HIT!!! \n {0}回目で「{1}」!!!",
  56. (count + matchWord.Count()),
  57. matchWord);
  58.  
  59. break;
  60. }
  61.  
  62. count++;
  63.  
  64. if (ProgressDisplay)
  65. {
  66. Console.Write(wordQueue.Dequeue());
  67. }
  68. else
  69. {
  70. wordQueue.Dequeue();
  71. }
  72.  
  73. wordQueue.Enqueue(GetRandomString(1)[0]);
  74. }
  75. }
  76.  
  77. private string GetRandomString(int length)
  78. {
  79. var sb = new StringBuilder(length);
  80. for (int i = 0; i < length; i++)
  81. sb.Append(baseChars[GetRandomNumber()]);
  82. return sb.ToString();
  83. }
  84.  
  85. private int GetRandomNumber()
  86. {
  87. var bs = new byte[4];
  88. var rng = new RNGCryptoServiceProvider();
  89. rng.GetBytes(bs);
  90. var num = BitConverter.ToInt32(bs, 0);
  91. return Math.Abs(num % baseChars.Length);
  92. }
  93. }
  94. }
  95.  
  96. public class Test
  97. {
  98. public static void Main()
  99. {
  100. // var r = new Util.RandomWordMatch();
  101. var r = new Util.RandomWordMatch("145");
  102. r.Start();
  103. }
  104. }
Success #stdin #stdout 0.06s 24440KB
stdin
Standard input is empty
stdout
5541451111154151415551555154145155154441115554455454541541454545511514554515445541415144141455444541415454545555511555415554554541111541544551451555411544441414545151411111111555545441115141144545541555555455144441511111441511454415114114114555141141451415544411115415155444545114111514111141544144141151154514451414115154441551144514144551544414541115544114145551441114154144555411115151451444445541154451115444144411411454444141515551151411545411554544551511111444414415445511151415454544454555154151151415144514544545414111455415551414445554544444544411441154451511144455114514

    HIT!!! 
    580回目で「114514」!!!