文字列のいくつかの単語をランダム化する関数を作成する必要があります。例えば:
[Hello|Hi] guys. This is my [code|string]
関数は以下を返す必要があります。
Hello guys. This is my code
また
Hi guys. This is my string
文字列のいくつかの単語をランダム化する関数を作成する必要があります。例えば:
[Hello|Hi] guys. This is my [code|string]
関数は以下を返す必要があります。
Hello guys. This is my code
また
Hi guys. This is my string
次のような乱数ジェネレーターを取得できます。
var rand = new Random();
文字列の解析とすべてのオプションの取得については、調べることをお勧めしますSystem.Text.RegularExpressions
これまでの他の回答では、さまざまなプレースホルダーに対して既に 1 つまたは 2 つのオプションがある場合に、ランダムな文字列を取得する方法を示しました。それらは問題ありませんが、書き出すのはかなり退屈で面倒です。OPが提供したようなランダムな文字列「テンプレート」を取得できるパーサーを作成し、それを使用してランダムな文字列を生成する方がはるかに優れています。
ここに私がまとめた簡単なものがあります:
using System;
using System.Text.RegularExpressions;
namespace StackOverLoadTest {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
var s = new RandomString("[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week].");
for (int i = 0; i < 10; i++)
Console.WriteLine(s);
}
}
public class RandomString {
private Random _rnd = new Random();
private static Regex _rex = new Regex(@"\[ ( \|? (?<option>[^]|]+) )+ \]", System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
string _template;
public RandomString(string template) {
_template = template;
}
public override string ToString() {
return _rex.Replace(_template, GetRandomOption);
}
public string GetRandomOption(Match m) {
var options = m.Groups["option"].Captures;
int choice = _rnd.Next(0, options.Count);
return options[choice].Value;
}
}
}
ご覧のとおり、テンプレートを使用して新しい RandomString オブジェクトを作成します。次に、必要な回数だけ ToString() 関数を呼び出すだけで、そのたびにオプションの新しいランダム順列が得られます。
任意の数のオプションで任意の数のプレースホルダーを使用できます (0 を除く)。この例で使用した文字列テンプレートは次のとおりです。
"[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week]."
上記のコードを実行すると、次の結果が得られました。
Hey guys. I should walk to the park sometime today.
Hi guys. We should walk to the farm sometime today.
Hi guys. He should walk to the field sometime next week.
Hey guys. You should walk to the park sometime next week.
Hi guys. She should walk to the farm sometime next week.
Hey guys. We should walk to the field sometime tomorrow.
Hi guys. I should walk to the farm sometime today.
Hey guys. He should walk to the field sometime tomorrow.
Hi guys. You should walk to the park sometime next week.
Hi guys. I should walk to the farm sometime today.
私はこのアプローチを使用しました:
Random rand = new Random();
int val = rand.Next(0, 100);
Console.WriteLine(
"{0} guys. This is my {1}",
val >= 50 ? "Hi" : "Hello",
val < 50 ? "code" : "string");
どの単語が書かれているかに 50% ~ 50% の確率を与えたので、それが表示され>= 50
ます< 50
。あなたはそれを変更することができます。
完全な文ではなく、それぞれの単語をランダム化したい場合 (上記のコードでは 2 つのバリエーションしか得られません)、コードをいじるか、コメントして修正してください。
ノート:
実際には 50%-50% ではありません。混乱させたくなかったのですが、そのようにしたい場合は、最初の条件を>= 49
*にする必要があります
条件 ( ) の構文は、三項 if 演算子condition ? statement : statement
と呼ばれます。
これを試して :
private string randArr(String[] _arr)
{
Random _rnd = new Random(DateTime.Now.GetHashCode());
return _arr[_rnd.Next(_arr.length)];
}
文字列値の配列を指定して呼び出すだけです。このような :
String.Format("{0} guys. This is my {1}", randArr(["Hello","Hi"]), randArr(["code","string"]));